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 e9ce266e 2022-03-07 op #include <stdint.h>
34 e9ce266e 2022-03-07 op #include <stdio.h>
35 e9ce266e 2022-03-07 op #include <stdlib.h>
36 e9ce266e 2022-03-07 op #include <string.h>
37 e9ce266e 2022-03-07 op #include <unistd.h>
38 e9ce266e 2022-03-07 op #include <imsg.h>
40 e9ce266e 2022-03-07 op #include "got_error.h"
41 e9ce266e 2022-03-07 op #include "got_object.h"
42 e9ce266e 2022-03-07 op #include "got_path.h"
43 e9ce266e 2022-03-07 op #include "got_reference.h"
44 e9ce266e 2022-03-07 op #include "got_cancel.h"
45 e9ce266e 2022-03-07 op #include "got_worktree.h"
46 55e9459f 2022-06-19 op #include "got_repository.h"
47 e9ce266e 2022-03-07 op #include "got_opentemp.h"
48 e9ce266e 2022-03-07 op #include "got_patch.h"
49 4b752015 2022-06-30 stsp #include "got_diff.h"
51 e9ce266e 2022-03-07 op #include "got_lib_delta.h"
52 55e9459f 2022-06-19 op #include "got_lib_diff.h"
53 e9ce266e 2022-03-07 op #include "got_lib_object.h"
54 e9ce266e 2022-03-07 op #include "got_lib_privsep.h"
55 55e9459f 2022-06-19 op #include "got_lib_sha1.h"
57 e9ce266e 2022-03-07 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
59 e9ce266e 2022-03-07 op struct got_patch_hunk {
60 e9ce266e 2022-03-07 op STAILQ_ENTRY(got_patch_hunk) entries;
61 60aa1fa0 2022-03-17 op const struct got_error *err;
62 a92a2042 2022-07-02 op int ws_mangled;
67 35095610 2022-06-14 op int old_lines;
69 35095610 2022-06-14 op int new_lines;
75 60aa1fa0 2022-03-17 op STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
76 e9ce266e 2022-03-07 op struct got_patch {
81 55e9459f 2022-06-19 op char blob[41];
82 60aa1fa0 2022-03-17 op struct got_patch_hunk_head head;
85 b22138f5 2022-03-16 op struct patch_args {
86 b22138f5 2022-03-16 op got_patch_progress_cb progress_cb;
87 b22138f5 2022-03-16 op void *progress_arg;
88 60aa1fa0 2022-03-17 op struct got_patch_hunk_head *head;
92 b2b3fce1 2022-10-29 op apply_umask(mode_t mode)
96 b2b3fce1 2022-10-29 op um = umask(000);
98 b2b3fce1 2022-10-29 op return mode & ~um;
101 e9ce266e 2022-03-07 op static const struct got_error *
102 e9ce266e 2022-03-07 op send_patch(struct imsgbuf *ibuf, int fd)
104 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
106 e9ce266e 2022-03-07 op if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
107 e9ce266e 2022-03-07 op NULL, 0) == -1) {
108 e9ce266e 2022-03-07 op err = got_error_from_errno(
109 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_FILE");
114 08f44789 2022-07-07 op return got_privsep_flush_imsg(ibuf);
118 e9ce266e 2022-03-07 op patch_free(struct got_patch *p)
120 e9ce266e 2022-03-07 op struct got_patch_hunk *h;
123 e9ce266e 2022-03-07 op while (!STAILQ_EMPTY(&p->head)) {
124 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
125 e9ce266e 2022-03-07 op STAILQ_REMOVE_HEAD(&p->head, entries);
127 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i)
128 e9ce266e 2022-03-07 op free(h->lines[i]);
129 e9ce266e 2022-03-07 op free(h->lines);
133 e9ce266e 2022-03-07 op free(p->new);
134 e9ce266e 2022-03-07 op free(p->old);
136 0e07a2a1 2022-06-13 op memset(p, 0, sizeof(*p));
137 0e07a2a1 2022-06-13 op STAILQ_INIT(&p->head);
140 e9ce266e 2022-03-07 op static const struct got_error *
141 e9ce266e 2022-03-07 op pushline(struct got_patch_hunk *h, const char *line)
144 e9ce266e 2022-03-07 op size_t newcap;
146 e9ce266e 2022-03-07 op if (h->len == h->cap) {
147 e9ce266e 2022-03-07 op if ((newcap = h->cap * 1.5) == 0)
149 e9ce266e 2022-03-07 op t = recallocarray(h->lines, h->cap, newcap,
150 e9ce266e 2022-03-07 op sizeof(h->lines[0]));
151 e9ce266e 2022-03-07 op if (t == NULL)
152 e9ce266e 2022-03-07 op return got_error_from_errno("recallocarray");
153 e9ce266e 2022-03-07 op h->lines = t;
154 e9ce266e 2022-03-07 op h->cap = newcap;
157 e9ce266e 2022-03-07 op if ((t = strdup(line)) == NULL)
158 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
160 e9ce266e 2022-03-07 op h->lines[h->len++] = t;
164 e9ce266e 2022-03-07 op static const struct got_error *
165 9d6cabd5 2022-04-07 op recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
167 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
168 e9ce266e 2022-03-07 op struct imsg imsg;
169 e9ce266e 2022-03-07 op struct got_imsg_patch_hunk hdr;
170 e9ce266e 2022-03-07 op struct got_imsg_patch patch;
171 e9ce266e 2022-03-07 op struct got_patch_hunk *h = NULL;
172 e9ce266e 2022-03-07 op size_t datalen;
173 b2832778 2022-04-23 op int lastmode = -1;
175 e9ce266e 2022-03-07 op memset(p, 0, sizeof(*p));
176 e9ce266e 2022-03-07 op STAILQ_INIT(&p->head);
178 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
181 e9ce266e 2022-03-07 op if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
185 e9ce266e 2022-03-07 op if (imsg.hdr.type != GOT_IMSG_PATCH) {
186 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
189 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
190 e9ce266e 2022-03-07 op if (datalen != sizeof(patch)) {
191 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
194 e9ce266e 2022-03-07 op memcpy(&patch, imsg.data, sizeof(patch));
196 99b94ad7 2022-06-13 op if (patch.old[sizeof(patch.old)-1] != '\0' ||
197 55e9459f 2022-06-19 op patch.new[sizeof(patch.new)-1] != '\0' ||
198 d8b5af43 2022-06-19 op patch.cid[sizeof(patch.cid)-1] != '\0' ||
199 55e9459f 2022-06-19 op patch.blob[sizeof(patch.blob)-1] != '\0') {
200 99b94ad7 2022-06-13 op err = got_error(GOT_ERR_PRIVSEP_LEN);
204 497a5915 2022-06-27 op if (*patch.cid != '\0')
205 d8b5af43 2022-06-19 op strlcpy(p->cid, patch.cid, sizeof(p->cid));
207 497a5915 2022-06-27 op if (*patch.blob != '\0')
208 d8b5af43 2022-06-19 op strlcpy(p->blob, patch.blob, sizeof(p->blob));
210 611e5fc2 2022-09-21 mark p->xbit = patch.xbit;
212 9d6cabd5 2022-04-07 op /* automatically set strip=1 for git-style diffs */
213 9d6cabd5 2022-04-07 op if (strip == -1 && patch.git &&
214 9d6cabd5 2022-04-07 op (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
215 9d6cabd5 2022-04-07 op (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
218 9d6cabd5 2022-04-07 op /* prefer the new name if not /dev/null for not git-style diffs */
219 9d6cabd5 2022-04-07 op if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
220 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.new, strip);
223 9d6cabd5 2022-04-07 op } else if (*patch.old != '\0') {
224 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.old, strip);
229 9d6cabd5 2022-04-07 op if (*patch.new != '\0') {
230 9d6cabd5 2022-04-07 op err = got_path_strip(&p->new, patch.new, strip);
235 b95c53df 2022-03-12 op if (p->old == NULL && p->new == NULL) {
236 b95c53df 2022-03-12 op err = got_error(GOT_ERR_PATCH_MALFORMED);
240 e9ce266e 2022-03-07 op imsg_free(&imsg);
245 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
247 0e07a2a1 2022-06-13 op patch_free(p);
251 2399b53d 2022-06-14 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
252 e9ce266e 2022-03-07 op switch (imsg.hdr.type) {
253 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_DONE:
254 be33dff7 2022-05-13 op if (h != NULL && h->len == 0)
255 be33dff7 2022-05-13 op err = got_error(GOT_ERR_PATCH_MALFORMED);
257 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_HUNK:
258 be33dff7 2022-05-13 op if (h != NULL &&
259 be33dff7 2022-05-13 op (h->len == 0 || h->old_nonl || h->new_nonl)) {
260 b3c57ab2 2022-03-22 op err = got_error(GOT_ERR_PATCH_MALFORMED);
263 b2832778 2022-04-23 op lastmode = -1;
264 e9ce266e 2022-03-07 op if (datalen != sizeof(hdr)) {
265 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
268 e9ce266e 2022-03-07 op memcpy(&hdr, imsg.data, sizeof(hdr));
269 5b7126c5 2022-06-14 op if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
270 5b7126c5 2022-06-14 op err = got_error(GOT_ERR_PRIVSEP_LEN);
273 5b7126c5 2022-06-14 op if ((h = calloc(1, sizeof(*h))) == NULL) {
274 5b7126c5 2022-06-14 op err = got_error_from_errno("calloc");
277 e9ce266e 2022-03-07 op h->old_from = hdr.oldfrom;
278 e9ce266e 2022-03-07 op h->old_lines = hdr.oldlines;
279 e9ce266e 2022-03-07 op h->new_from = hdr.newfrom;
280 e9ce266e 2022-03-07 op h->new_lines = hdr.newlines;
281 e9ce266e 2022-03-07 op STAILQ_INSERT_TAIL(&p->head, h, entries);
283 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_LINE:
284 e9ce266e 2022-03-07 op if (h == NULL) {
285 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
288 e9ce266e 2022-03-07 op t = imsg.data;
289 b3c57ab2 2022-03-22 op /* at least one char */
290 e9ce266e 2022-03-07 op if (datalen < 2 || t[datalen-1] != '\0') {
291 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
294 b3c57ab2 2022-03-22 op if (*t != ' ' && *t != '-' && *t != '+' &&
295 b3c57ab2 2022-03-22 op *t != '\\') {
296 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
300 b2832778 2022-04-23 op if (*t != '\\')
301 b3c57ab2 2022-03-22 op err = pushline(h, t);
302 b2832778 2022-04-23 op else if (lastmode == '-')
303 b2832778 2022-04-23 op h->old_nonl = 1;
304 b2832778 2022-04-23 op else if (lastmode == '+')
305 b2832778 2022-04-23 op h->new_nonl = 1;
307 b2832778 2022-04-23 op err = got_error(GOT_ERR_PATCH_MALFORMED);
312 b2832778 2022-04-23 op lastmode = *t;
315 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
319 e9ce266e 2022-03-07 op imsg_free(&imsg);
324 0e07a2a1 2022-06-13 op patch_free(p);
326 e9ce266e 2022-03-07 op imsg_free(&imsg);
331 38d61ead 2022-07-23 op reverse_patch(struct got_patch *p)
333 38d61ead 2022-07-23 op struct got_patch_hunk *h;
337 38d61ead 2022-07-23 op STAILQ_FOREACH(h, &p->head, entries) {
338 38d61ead 2022-07-23 op tmp = h->old_from;
339 38d61ead 2022-07-23 op h->old_from = h->new_from;
340 38d61ead 2022-07-23 op h->new_from = tmp;
342 38d61ead 2022-07-23 op tmp = h->old_lines;
343 38d61ead 2022-07-23 op h->old_lines = h->new_lines;
344 38d61ead 2022-07-23 op h->new_lines = tmp;
346 38d61ead 2022-07-23 op tmp = h->old_nonl;
347 38d61ead 2022-07-23 op h->old_nonl = h->new_nonl;
348 38d61ead 2022-07-23 op h->new_nonl = tmp;
350 38d61ead 2022-07-23 op for (i = 0; i < h->len; ++i) {
351 38d61ead 2022-07-23 op if (*h->lines[i] == '+')
352 38d61ead 2022-07-23 op *h->lines[i] = '-';
353 38d61ead 2022-07-23 op else if (*h->lines[i] == '-')
354 38d61ead 2022-07-23 op *h->lines[i] = '+';
360 e9ce266e 2022-03-07 op * Copy data from orig starting at copypos until pos into tmp.
361 e9ce266e 2022-03-07 op * If pos is -1, copy until EOF.
363 e9ce266e 2022-03-07 op static const struct got_error *
364 e9ce266e 2022-03-07 op copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
366 e9ce266e 2022-03-07 op char buf[BUFSIZ];
367 e9ce266e 2022-03-07 op size_t len, r, w;
369 e45f7eba 2022-05-14 naddy if (fseeko(orig, copypos, SEEK_SET) == -1)
370 e45f7eba 2022-05-14 naddy return got_error_from_errno("fseeko");
372 e9ce266e 2022-03-07 op while (pos == -1 || copypos < pos) {
373 e9ce266e 2022-03-07 op len = sizeof(buf);
375 e9ce266e 2022-03-07 op len = MIN(len, (size_t)pos - copypos);
376 e9ce266e 2022-03-07 op r = fread(buf, 1, len, orig);
377 e9ce266e 2022-03-07 op if (r != len && ferror(orig))
378 e9ce266e 2022-03-07 op return got_error_from_errno("fread");
379 e9ce266e 2022-03-07 op w = fwrite(buf, 1, r, tmp);
381 e9ce266e 2022-03-07 op return got_error_from_errno("fwrite");
382 e9ce266e 2022-03-07 op copypos += len;
383 e9ce266e 2022-03-07 op if (r != len && feof(orig)) {
384 e9ce266e 2022-03-07 op if (pos == -1)
386 60aa1fa0 2022-03-17 op return got_error(GOT_ERR_HUNK_FAILED);
392 445d38d7 2022-08-01 op static int linecmp(const char *, const char *, int *);
394 e9ce266e 2022-03-07 op static const struct got_error *
395 35095610 2022-06-14 op locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
397 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
398 e9ce266e 2022-03-07 op char *line = NULL;
399 e9ce266e 2022-03-07 op char mode = *h->lines[0];
400 e9ce266e 2022-03-07 op size_t linesize = 0;
401 e9ce266e 2022-03-07 op ssize_t linelen;
402 e9ce266e 2022-03-07 op off_t match = -1;
403 445d38d7 2022-08-01 op int mangled = 0, match_lineno = -1;
406 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
407 e9ce266e 2022-03-07 op if (linelen == -1) {
408 e9ce266e 2022-03-07 op if (ferror(orig))
409 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
410 e9ce266e 2022-03-07 op else if (match == -1)
411 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
414 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
415 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
418 445d38d7 2022-08-01 op if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
419 445d38d7 2022-08-01 op (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
420 e9ce266e 2022-03-07 op (mode == '+' && *lineno == h->old_from)) {
421 e9ce266e 2022-03-07 op match = ftello(orig);
422 e9ce266e 2022-03-07 op if (match == -1) {
423 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
426 e9ce266e 2022-03-07 op match -= linelen;
427 e9ce266e 2022-03-07 op match_lineno = (*lineno)-1;
430 d7c808b7 2022-08-01 op if (*lineno >= h->old_from && match != -1) {
432 d7c808b7 2022-08-01 op h->ws_mangled = 1;
437 e9ce266e 2022-03-07 op if (err == NULL) {
438 33df9995 2022-03-11 op *pos = match;
439 e9ce266e 2022-03-07 op *lineno = match_lineno;
440 e45f7eba 2022-05-14 naddy if (fseeko(orig, match, SEEK_SET) == -1)
441 e45f7eba 2022-05-14 naddy err = got_error_from_errno("fseeko");
449 a92a2042 2022-07-02 op linecmp(const char *a, const char *b, int *mangled)
453 a92a2042 2022-07-02 op *mangled = 0;
454 a92a2042 2022-07-02 op c = strcmp(a, b);
458 a92a2042 2022-07-02 op *mangled = 1;
460 2c986b8f 2022-07-04 op while (*a == '\t' || *a == ' ' || *a == '\f')
462 2c986b8f 2022-07-04 op while (*b == '\t' || *b == ' ' || *b == '\f')
464 2c986b8f 2022-07-04 op if (*a == '\0' || *a != *b)
469 a92a2042 2022-07-02 op return *a - *b;
472 e9ce266e 2022-03-07 op static const struct got_error *
473 e9ce266e 2022-03-07 op test_hunk(FILE *orig, struct got_patch_hunk *h)
475 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
476 e9ce266e 2022-03-07 op char *line = NULL;
477 e9ce266e 2022-03-07 op size_t linesize = 0, i = 0;
478 e9ce266e 2022-03-07 op ssize_t linelen;
481 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
482 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
487 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
488 e9ce266e 2022-03-07 op if (linelen == -1) {
489 e9ce266e 2022-03-07 op if (ferror(orig))
490 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
492 e9ce266e 2022-03-07 op err = got_error(
493 60aa1fa0 2022-03-17 op GOT_ERR_HUNK_FAILED);
496 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
497 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
498 a92a2042 2022-07-02 op if (linecmp(h->lines[i] + 1, line, &mangled)) {
499 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
503 a92a2042 2022-07-02 op h->ws_mangled = 1;
513 e9ce266e 2022-03-07 op static const struct got_error *
514 a92a2042 2022-07-02 op apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
517 a92a2042 2022-07-02 op const struct got_error *err = NULL;
518 a92a2042 2022-07-02 op const char *t;
519 a92a2042 2022-07-02 op size_t linesize = 0, i, new = 0;
520 a92a2042 2022-07-02 op char *line = NULL;
522 a92a2042 2022-07-02 op ssize_t linelen;
524 a92a2042 2022-07-02 op if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
525 a92a2042 2022-07-02 op return got_error_from_errno("fseeko");
527 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
528 a92a2042 2022-07-02 op switch (mode = *h->lines[i]) {
532 a92a2042 2022-07-02 op if (orig != NULL) {
533 a92a2042 2022-07-02 op linelen = getline(&line, &linesize, orig);
534 a92a2042 2022-07-02 op if (linelen == -1) {
535 a92a2042 2022-07-02 op err = got_error_from_errno("getline");
538 a92a2042 2022-07-02 op if (line[linelen - 1] == '\n')
539 a92a2042 2022-07-02 op line[linelen - 1] = '\0';
542 a92a2042 2022-07-02 op t = h->lines[i] + 1;
543 a92a2042 2022-07-02 op if (mode == '-')
545 a92a2042 2022-07-02 op if (fprintf(tmp, "%s\n", t) < 0) {
546 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
552 a92a2042 2022-07-02 op if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
553 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
556 b2832778 2022-04-23 op if (new != h->new_lines || !h->new_nonl) {
557 a92a2042 2022-07-02 op if (fprintf(tmp, "\n") < 0) {
558 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
571 6e96b326 2022-03-12 op static const struct got_error *
572 5dffb1a1 2022-07-02 op patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
574 6e96b326 2022-03-12 op const struct got_error *err = NULL;
575 6e96b326 2022-03-12 op struct got_patch_hunk *h;
576 2be5e1a2 2022-03-16 op struct stat sb;
577 35095610 2022-06-14 op int lineno = 0;
578 6e96b326 2022-03-12 op off_t copypos, pos;
579 6e96b326 2022-03-12 op char *line = NULL;
580 6e96b326 2022-03-12 op size_t linesize = 0;
581 6e96b326 2022-03-12 op ssize_t linelen;
583 e9ce266e 2022-03-07 op if (p->old == NULL) { /* create */
584 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
585 6e96b326 2022-03-12 op if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
586 6e96b326 2022-03-12 op return got_error(GOT_ERR_PATCH_MALFORMED);
587 a92a2042 2022-07-02 op return apply_hunk(orig, tmp, h, &lineno, 0);
590 684a9a6c 2022-12-31 op /* When deleting binary files there are no hunks to apply. */
591 684a9a6c 2022-12-31 op if (p->new == NULL && STAILQ_EMPTY(&p->head))
594 827bcd6c 2022-06-19 op if (fstat(fileno(orig), &sb) == -1)
595 827bcd6c 2022-06-19 op return got_error_from_errno("fstat");
598 e9ce266e 2022-03-07 op STAILQ_FOREACH(h, &p->head, entries) {
600 33df9995 2022-03-11 op err = locate_hunk(orig, h, &pos, &lineno);
601 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
602 60aa1fa0 2022-03-17 op h->err = err;
603 e9ce266e 2022-03-07 op if (err != NULL)
605 05737d49 2022-06-19 op err = copy(tmp, orig, copypos, pos);
606 e9ce266e 2022-03-07 op if (err != NULL)
608 e9ce266e 2022-03-07 op copypos = pos;
610 e9ce266e 2022-03-07 op err = test_hunk(orig, h);
611 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
613 e9ce266e 2022-03-07 op * try to apply the hunk again starting the search
614 e9ce266e 2022-03-07 op * after the previous partial match.
616 827bcd6c 2022-06-19 op if (fseeko(orig, pos, SEEK_SET) == -1)
617 827bcd6c 2022-06-19 op return got_error_from_errno("fseeko");
618 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
619 827bcd6c 2022-06-19 op if (linelen == -1)
620 827bcd6c 2022-06-19 op return got_error_from_errno("getline");
622 e9ce266e 2022-03-07 op goto tryagain;
624 e9ce266e 2022-03-07 op if (err != NULL)
627 60aa1fa0 2022-03-17 op if (lineno + 1 != h->old_from)
628 60aa1fa0 2022-03-17 op h->offset = lineno + 1 - h->old_from;
630 a92a2042 2022-07-02 op err = apply_hunk(orig, tmp, h, &lineno, pos);
631 e9ce266e 2022-03-07 op if (err != NULL)
634 e9ce266e 2022-03-07 op copypos = ftello(orig);
635 827bcd6c 2022-06-19 op if (copypos == -1)
636 827bcd6c 2022-06-19 op return got_error_from_errno("ftello");
639 60aa1fa0 2022-03-17 op if (p->new == NULL && sb.st_size != copypos) {
640 60aa1fa0 2022-03-17 op h = STAILQ_FIRST(&p->head);
641 60aa1fa0 2022-03-17 op h->err = got_error(GOT_ERR_HUNK_FAILED);
642 60aa1fa0 2022-03-17 op err = h->err;
643 05737d49 2022-06-19 op } else if (!feof(orig))
644 e9ce266e 2022-03-07 op err = copy(tmp, orig, copypos, -1);
649 dbda770b 2022-03-13 op static const struct got_error *
650 60aa1fa0 2022-03-17 op report_progress(struct patch_args *pa, const char *old, const char *new,
651 60aa1fa0 2022-03-17 op unsigned char status, const struct got_error *orig_error)
653 60aa1fa0 2022-03-17 op const struct got_error *err;
654 60aa1fa0 2022-03-17 op struct got_patch_hunk *h;
656 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, status,
657 a92a2042 2022-07-02 op orig_error, 0, 0, 0, 0, 0, 0, NULL);
661 60aa1fa0 2022-03-17 op STAILQ_FOREACH(h, pa->head, entries) {
662 a92a2042 2022-07-02 op if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
665 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
666 60aa1fa0 2022-03-17 op h->old_from, h->old_lines, h->new_from, h->new_lines,
667 a92a2042 2022-07-02 op h->offset, h->ws_mangled, h->err);
675 60aa1fa0 2022-03-17 op static const struct got_error *
676 b22138f5 2022-03-16 op patch_delete(void *arg, unsigned char status, unsigned char staged_status,
677 b22138f5 2022-03-16 op const char *path)
679 60aa1fa0 2022-03-17 op return report_progress(arg, path, NULL, status, NULL);
682 b22138f5 2022-03-16 op static const struct got_error *
683 b22138f5 2022-03-16 op patch_add(void *arg, unsigned char status, const char *path)
685 60aa1fa0 2022-03-17 op return report_progress(arg, NULL, path, status, NULL);
688 b22138f5 2022-03-16 op static const struct got_error *
689 55e9459f 2022-06-19 op open_blob(char **path, FILE **fp, const char *blobid,
690 55e9459f 2022-06-19 op struct got_repository *repo)
692 6e96b326 2022-03-12 op const struct got_error *err = NULL;
693 55e9459f 2022-06-19 op struct got_blob_object *blob = NULL;
694 db0dfdd7 2022-06-27 op struct got_object_id id, *idptr, *matched_id = NULL;
695 eb81bc23 2022-06-28 tracey int fd = -1;
698 55e9459f 2022-06-19 op *path = NULL;
700 db0dfdd7 2022-06-27 op if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
701 db0dfdd7 2022-06-27 op err = got_repo_match_object_id(&matched_id, NULL, blobid,
702 db0dfdd7 2022-06-27 op GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
706 db0dfdd7 2022-06-27 op idptr = matched_id;
708 db0dfdd7 2022-06-27 op if (!got_parse_sha1_digest(id.sha1, blobid))
709 db0dfdd7 2022-06-27 op return got_error(GOT_ERR_BAD_OBJ_ID_STR);
713 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
714 eb81bc23 2022-06-28 tracey if (fd == -1) {
715 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
716 eb81bc23 2022-06-28 tracey goto done;
719 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
723 b90054ed 2022-11-01 stsp err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
728 55e9459f 2022-06-19 op err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
733 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
734 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
736 55e9459f 2022-06-19 op got_object_blob_close(blob);
737 db0dfdd7 2022-06-27 op if (matched_id != NULL)
738 db0dfdd7 2022-06-27 op free(matched_id);
740 55e9459f 2022-06-19 op if (*fp != NULL)
742 55e9459f 2022-06-19 op if (*path != NULL)
743 55e9459f 2022-06-19 op unlink(*path);
746 55e9459f 2022-06-19 op *path = NULL;
751 55e9459f 2022-06-19 op static const struct got_error *
752 5f56d41e 2022-07-28 op prepare_merge(int *do_merge, char **apath, FILE **afile,
753 5f56d41e 2022-07-28 op struct got_worktree *worktree, struct got_repository *repo,
754 5f56d41e 2022-07-28 op struct got_patch *p, struct got_object_id *commit_id,
755 5f56d41e 2022-07-28 op struct got_tree_object *tree, const char *path)
757 5f56d41e 2022-07-28 op const struct got_error *err = NULL;
759 5f56d41e 2022-07-28 op *do_merge = 0;
760 5f56d41e 2022-07-28 op *apath = NULL;
761 5f56d41e 2022-07-28 op *afile = NULL;
763 5f56d41e 2022-07-28 op /* don't run the diff3 merge on creations/deletions */
764 5f56d41e 2022-07-28 op if (p->old == NULL || p->new == NULL)
767 5f56d41e 2022-07-28 op if (commit_id) {
768 5f56d41e 2022-07-28 op struct got_object_id *id;
770 5f56d41e 2022-07-28 op err = got_object_tree_find_path(&id, NULL, repo, tree, path);
773 5f56d41e 2022-07-28 op got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
774 5f56d41e 2022-07-28 op got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
776 5f56d41e 2022-07-28 op err = open_blob(apath, afile, p->blob, repo);
777 5f56d41e 2022-07-28 op *do_merge = err == NULL;
778 5f56d41e 2022-07-28 op } else if (*p->blob != '\0') {
779 5f56d41e 2022-07-28 op err = open_blob(apath, afile, p->blob, repo);
781 5f56d41e 2022-07-28 op * ignore failures to open this blob, we might have
782 5f56d41e 2022-07-28 op * parsed gibberish.
784 5f56d41e 2022-07-28 op if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
785 5f56d41e 2022-07-28 op err->code != GOT_ERR_NO_OBJ)
787 5f56d41e 2022-07-28 op *do_merge = err == NULL;
794 5f56d41e 2022-07-28 op static const struct got_error *
795 55e9459f 2022-06-19 op apply_patch(int *overlapcnt, struct got_worktree *worktree,
796 55e9459f 2022-06-19 op struct got_repository *repo, struct got_fileindex *fileindex,
797 55e9459f 2022-06-19 op const char *old, const char *new, struct got_patch *p, int nop,
798 5f56d41e 2022-07-28 op int reverse, struct got_object_id *commit_id,
799 5f56d41e 2022-07-28 op struct got_tree_object *tree, struct patch_args *pa,
800 38d61ead 2022-07-23 op got_cancel_cb cancel_cb, void *cancel_arg)
802 55e9459f 2022-06-19 op const struct got_error *err = NULL;
803 5dffb1a1 2022-07-02 op struct stat sb;
804 55e9459f 2022-06-19 op int do_merge = 0, file_renamed = 0;
805 d8b5af43 2022-06-19 op char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
806 ed3bff83 2022-04-23 op char *oldpath = NULL, *newpath = NULL;
807 a6072ec9 2022-10-15 stsp char *tmppath = NULL, *template = NULL;
808 55e9459f 2022-06-19 op char *apath = NULL, *mergepath = NULL;
809 55e9459f 2022-06-19 op FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
811 2be5e1a2 2022-03-16 op mode_t mode = GOT_DEFAULT_FILE_MODE;
813 55e9459f 2022-06-19 op *overlapcnt = 0;
815 5f56d41e 2022-07-28 op err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
816 5f56d41e 2022-07-28 op commit_id, tree, old);
820 615e455c 2022-07-27 op if (reverse && !do_merge)
821 615e455c 2022-07-27 op reverse_patch(p);
823 ed3bff83 2022-04-23 op if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
824 ed3bff83 2022-04-23 op old) == -1) {
825 ed3bff83 2022-04-23 op err = got_error_from_errno("asprintf");
829 ed3bff83 2022-04-23 op if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
830 ed3bff83 2022-04-23 op new) == -1) {
831 ed3bff83 2022-04-23 op err = got_error_from_errno("asprintf");
835 78f5ac24 2022-03-19 op file_renamed = strcmp(oldpath, newpath);
837 6e96b326 2022-03-12 op if (asprintf(&template, "%s/got-patch",
838 6e96b326 2022-03-12 op got_worktree_get_root_path(worktree)) == -1) {
839 6e96b326 2022-03-12 op err = got_error_from_errno(template);
843 5dffb1a1 2022-07-02 op if (p->old != NULL) {
844 5dffb1a1 2022-07-02 op if ((oldfile = fopen(oldpath, "r")) == NULL) {
845 5dffb1a1 2022-07-02 op err = got_error_from_errno2("open", oldpath);
848 5dffb1a1 2022-07-02 op if (fstat(fileno(oldfile), &sb) == -1) {
849 5dffb1a1 2022-07-02 op err = got_error_from_errno2("fstat", oldpath);
852 5dffb1a1 2022-07-02 op mode = sb.st_mode;
853 611e5fc2 2022-09-21 mark } else if (p->xbit)
854 611e5fc2 2022-09-21 mark mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
856 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpfile, template, "");
859 55e9459f 2022-06-19 op outfd = fileno(tmpfile);
860 5dffb1a1 2022-07-02 op err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
864 55e9459f 2022-06-19 op if (do_merge) {
865 497a5915 2022-06-27 op const char *type, *id;
867 55e9459f 2022-06-19 op if (fseeko(afile, 0, SEEK_SET) == -1 ||
868 55e9459f 2022-06-19 op fseeko(oldfile, 0, SEEK_SET) == -1 ||
869 55e9459f 2022-06-19 op fseeko(tmpfile, 0, SEEK_SET) == -1) {
870 55e9459f 2022-06-19 op err = got_error_from_errno("fseeko");
874 55e9459f 2022-06-19 op if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
875 55e9459f 2022-06-19 op err = got_error_from_errno("asprintf");
876 55e9459f 2022-06-19 op oldlabel = NULL;
880 55e9459f 2022-06-19 op if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
881 55e9459f 2022-06-19 op err = got_error_from_errno("asprintf");
882 55e9459f 2022-06-19 op newlabel = NULL;
886 497a5915 2022-06-27 op if (*p->cid != '\0') {
887 497a5915 2022-06-27 op type = "commit";
890 497a5915 2022-06-27 op type = "blob";
891 497a5915 2022-06-27 op id = p->blob;
894 497a5915 2022-06-27 op if (asprintf(&anclabel, "%s %s", type, id) == -1) {
895 d8b5af43 2022-06-19 op err = got_error_from_errno("asprintf");
896 d8b5af43 2022-06-19 op anclabel = NULL;
900 38d61ead 2022-07-23 op if (reverse) {
904 38d61ead 2022-07-23 op s = anclabel;
905 38d61ead 2022-07-23 op anclabel = newlabel;
906 38d61ead 2022-07-23 op newlabel = s;
909 38d61ead 2022-07-23 op afile = tmpfile;
913 b90054ed 2022-11-01 stsp err = got_opentemp_named(&mergepath, &mergefile, template, "");
916 55e9459f 2022-06-19 op outfd = fileno(mergefile);
918 55e9459f 2022-06-19 op err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
919 d8b5af43 2022-06-19 op oldfile, oldlabel, anclabel, newlabel,
920 55e9459f 2022-06-19 op GOT_DIFF_ALGORITHM_PATIENCE);
928 5b67f96e 2022-03-13 op if (p->old != NULL && p->new == NULL) {
929 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
930 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
934 b2b3fce1 2022-10-29 op if (fchmod(outfd, apply_umask(mode)) == -1) {
935 41a37a44 2022-04-23 op err = got_error_from_errno2("chmod", tmppath);
939 a6072ec9 2022-10-15 stsp if (mergepath) {
940 a6072ec9 2022-10-15 stsp err = got_path_move_file(mergepath, newpath);
943 a6072ec9 2022-10-15 stsp free(mergepath);
944 a6072ec9 2022-10-15 stsp mergepath = NULL;
946 a6072ec9 2022-10-15 stsp err = got_path_move_file(tmppath, newpath);
949 a6072ec9 2022-10-15 stsp free(tmppath);
950 a6072ec9 2022-10-15 stsp tmppath = NULL;
953 6e96b326 2022-03-12 op if (file_renamed) {
954 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
955 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
956 6e96b326 2022-03-12 op if (err == NULL)
957 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo,
958 f2dd7807 2022-05-17 op worktree, fileindex, patch_add,
961 78f5ac24 2022-03-19 op unlink(newpath);
962 78f5ac24 2022-03-19 op } else if (p->old == NULL) {
963 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo, worktree,
964 f2dd7807 2022-05-17 op fileindex, patch_add, pa);
966 78f5ac24 2022-03-19 op unlink(newpath);
967 55e9459f 2022-06-19 op } else if (*overlapcnt != 0)
968 55e9459f 2022-06-19 op err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
969 9802c41c 2022-06-21 op else if (do_merge)
970 9802c41c 2022-06-21 op err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
972 ed3bff83 2022-04-23 op err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
975 6f5cb1bd 2022-03-08 op free(template);
977 a6072ec9 2022-10-15 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
978 a6072ec9 2022-10-15 stsp err = got_error_from_errno("unlink");
979 55e9459f 2022-06-19 op if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
980 06c44edc 2022-06-15 stsp err = got_error_from_errno("fclose");
981 e9ce266e 2022-03-07 op free(tmppath);
983 ed3bff83 2022-04-23 op free(oldpath);
984 827bcd6c 2022-06-19 op if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
985 827bcd6c 2022-06-19 op err = got_error_from_errno("fclose");
987 a6072ec9 2022-10-15 stsp if (apath != NULL && unlink(apath) == -1 && err == NULL)
988 a6072ec9 2022-10-15 stsp err = got_error_from_errno("unlink");
989 55e9459f 2022-06-19 op if (afile != NULL && fclose(afile) == EOF && err == NULL)
990 55e9459f 2022-06-19 op err = got_error_from_errno("fclose");
993 a6072ec9 2022-10-15 stsp if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
994 a6072ec9 2022-10-15 stsp err = got_error_from_errno("unlink");
995 55e9459f 2022-06-19 op if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
996 55e9459f 2022-06-19 op err = got_error_from_errno("fclose");
997 55e9459f 2022-06-19 op free(mergepath);
999 ed3bff83 2022-04-23 op free(newpath);
1000 55e9459f 2022-06-19 op free(oldlabel);
1001 55e9459f 2022-06-19 op free(newlabel);
1002 d8b5af43 2022-06-19 op free(anclabel);
1006 e9ce266e 2022-03-07 op const struct got_error *
1007 e9ce266e 2022-03-07 op got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1008 5f56d41e 2022-07-28 op int nop, int strip, int reverse, struct got_object_id *commit_id,
1009 5f56d41e 2022-07-28 op got_patch_progress_cb progress_cb, void *progress_arg,
1010 5f56d41e 2022-07-28 op got_cancel_cb cancel_cb, void *cancel_arg)
1012 4bcdc895 2022-05-17 op const struct got_error *err = NULL, *complete_err = NULL;
1013 78f5ac24 2022-03-19 op struct got_fileindex *fileindex = NULL;
1014 5f56d41e 2022-07-28 op struct got_commit_object *commit = NULL;
1015 5f56d41e 2022-07-28 op struct got_tree_object *tree = NULL;
1016 f2dd7807 2022-05-17 op char *fileindex_path = NULL;
1017 60aa1fa0 2022-03-17 op char *oldpath, *newpath;
1018 e9ce266e 2022-03-07 op struct imsgbuf *ibuf;
1019 e9ce266e 2022-03-07 op int imsg_fds[2] = {-1, -1};
1020 55e9459f 2022-06-19 op int overlapcnt, done = 0, failed = 0;
1023 e9ce266e 2022-03-07 op ibuf = calloc(1, sizeof(*ibuf));
1024 e9ce266e 2022-03-07 op if (ibuf == NULL) {
1025 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
1029 e9ce266e 2022-03-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1030 e9ce266e 2022-03-07 op err = got_error_from_errno("socketpair");
1034 e9ce266e 2022-03-07 op pid = fork();
1035 e9ce266e 2022-03-07 op if (pid == -1) {
1036 e9ce266e 2022-03-07 op err = got_error_from_errno("fork");
1038 e9ce266e 2022-03-07 op } else if (pid == 0) {
1039 e9ce266e 2022-03-07 op got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1041 e9ce266e 2022-03-07 op /* not reached */
1044 e9ce266e 2022-03-07 op if (close(imsg_fds[1]) == -1) {
1045 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1048 e9ce266e 2022-03-07 op imsg_fds[1] = -1;
1049 e9ce266e 2022-03-07 op imsg_init(ibuf, imsg_fds[0]);
1051 e9ce266e 2022-03-07 op err = send_patch(ibuf, fd);
1056 f2dd7807 2022-05-17 op err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1061 5f56d41e 2022-07-28 op if (commit_id) {
1062 5f56d41e 2022-07-28 op err = got_object_open_as_commit(&commit, repo, commit_id);
1066 5f56d41e 2022-07-28 op err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1071 e9ce266e 2022-03-07 op while (!done && err == NULL) {
1072 e9ce266e 2022-03-07 op struct got_patch p;
1073 60aa1fa0 2022-03-17 op struct patch_args pa;
1075 60aa1fa0 2022-03-17 op pa.progress_cb = progress_cb;
1076 60aa1fa0 2022-03-17 op pa.progress_arg = progress_arg;
1077 60aa1fa0 2022-03-17 op pa.head = &p.head;
1079 9d6cabd5 2022-04-07 op err = recv_patch(ibuf, &done, &p, strip);
1080 e9ce266e 2022-03-07 op if (err || done)
1083 78f5ac24 2022-03-19 op err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1084 78f5ac24 2022-03-19 op &newpath, worktree, repo, fileindex);
1085 78f5ac24 2022-03-19 op if (err == NULL)
1086 55e9459f 2022-06-19 op err = apply_patch(&overlapcnt, worktree, repo,
1087 38d61ead 2022-07-23 op fileindex, oldpath, newpath, &p, nop, reverse,
1088 5f56d41e 2022-07-28 op commit_id, tree, &pa, cancel_cb, cancel_arg);
1089 60aa1fa0 2022-03-17 op if (err != NULL) {
1091 60aa1fa0 2022-03-17 op /* recoverable errors */
1092 60aa1fa0 2022-03-17 op if (err->code == GOT_ERR_FILE_STATUS ||
1093 60aa1fa0 2022-03-17 op (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1094 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
1095 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, err);
1096 60aa1fa0 2022-03-17 op else if (err->code == GOT_ERR_HUNK_FAILED)
1097 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
1098 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, NULL);
1100 55e9459f 2022-06-19 op if (overlapcnt != 0)
1103 60aa1fa0 2022-03-17 op free(oldpath);
1104 60aa1fa0 2022-03-17 op free(newpath);
1105 e9ce266e 2022-03-07 op patch_free(&p);
1112 4bcdc895 2022-05-17 op if (fileindex != NULL)
1113 4bcdc895 2022-05-17 op complete_err = got_worktree_patch_complete(fileindex,
1114 4bcdc895 2022-05-17 op fileindex_path);
1115 f2dd7807 2022-05-17 op if (complete_err && err == NULL)
1116 f2dd7807 2022-05-17 op err = complete_err;
1117 4bcdc895 2022-05-17 op free(fileindex_path);
1119 5f56d41e 2022-07-28 op got_object_tree_close(tree);
1121 5f56d41e 2022-07-28 op got_object_commit_close(commit);
1122 e9ce266e 2022-03-07 op if (fd != -1 && close(fd) == -1 && err == NULL)
1123 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1124 e9ce266e 2022-03-07 op if (ibuf != NULL)
1125 e9ce266e 2022-03-07 op imsg_clear(ibuf);
1126 e9ce266e 2022-03-07 op if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1127 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1128 e9ce266e 2022-03-07 op if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1129 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1130 60aa1fa0 2022-03-17 op if (err == NULL && failed)
1131 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_PATCH_FAILED);