2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Things that we may want to support:
19 * + support indented patches?
20 * + support other kinds of patches?
23 #include <sys/types.h>
24 #include <sys/queue.h>
25 #include <sys/socket.h>
41 #include "got_error.h"
42 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
47 #include "got_repository.h"
48 #include "got_opentemp.h"
49 #include "got_patch.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_diff.h"
54 #include "got_lib_object.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_hash.h"
59 #define MIN(a, b) ((a) < (b) ? (a) : (b))
62 struct got_patch_line {
68 struct got_patch_hunk {
69 STAILQ_ENTRY(got_patch_hunk) entries;
70 const struct got_error *err;
81 struct got_patch_line *lines;
84 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
91 struct got_patch_hunk_head head;
95 got_patch_progress_cb progress_cb;
97 struct got_patch_hunk_head *head;
101 apply_umask(mode_t mode)
110 static const struct got_error *
111 send_patch(struct imsgbuf *ibuf, int fd)
113 const struct got_error *err = NULL;
115 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
117 err = got_error_from_errno(
118 "imsg_compose GOT_IMSG_PATCH_FILE");
123 return got_privsep_flush_imsg(ibuf);
127 patch_free(struct got_patch *p)
129 struct got_patch_hunk *h;
132 while (!STAILQ_EMPTY(&p->head)) {
133 h = STAILQ_FIRST(&p->head);
134 STAILQ_REMOVE_HEAD(&p->head, entries);
136 for (i = 0; i < h->len; ++i)
137 free(h->lines[i].line);
145 memset(p, 0, sizeof(*p));
146 STAILQ_INIT(&p->head);
149 static const struct got_error *
150 pushline(struct got_patch_hunk *h, const char *line, size_t len)
155 if (h->len == h->cap) {
156 if ((newcap = h->cap * 1.5) == 0)
158 t = recallocarray(h->lines, h->cap, newcap,
159 sizeof(h->lines[0]));
161 return got_error_from_errno("recallocarray");
166 if ((t = malloc(len - 1)) == NULL)
167 return got_error_from_errno("malloc");
168 memcpy(t, line + 1, len - 1); /* skip the line type */
170 h->lines[h->len].mode = *line;
171 h->lines[h->len].line = t;
172 h->lines[h->len].len = len - 2; /* line type and trailing NUL */
177 static const struct got_error *
178 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
180 const struct got_error *err = NULL;
182 struct got_imsg_patch_hunk hdr;
183 struct got_imsg_patch patch;
184 struct got_patch_hunk *h = NULL;
188 memset(p, 0, sizeof(*p));
189 STAILQ_INIT(&p->head);
191 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
194 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
198 if (imsg.hdr.type != GOT_IMSG_PATCH) {
199 err = got_error(GOT_ERR_PRIVSEP_MSG);
202 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
203 if (datalen != sizeof(patch)) {
204 err = got_error(GOT_ERR_PRIVSEP_LEN);
207 memcpy(&patch, imsg.data, sizeof(patch));
209 if (patch.old[sizeof(patch.old)-1] != '\0' ||
210 patch.new[sizeof(patch.new)-1] != '\0' ||
211 patch.cid[sizeof(patch.cid)-1] != '\0' ||
212 patch.blob[sizeof(patch.blob)-1] != '\0') {
213 err = got_error(GOT_ERR_PRIVSEP_LEN);
217 if (*patch.cid != '\0')
218 strlcpy(p->cid, patch.cid, sizeof(p->cid));
220 if (*patch.blob != '\0')
221 strlcpy(p->blob, patch.blob, sizeof(p->blob));
223 p->xbit = patch.xbit;
225 /* automatically set strip=1 for git-style diffs */
226 if (strip == -1 && patch.git &&
227 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
228 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
231 /* prefer the new name if not /dev/null for not git-style diffs */
232 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
233 err = got_path_strip(&p->old, patch.new, strip);
236 } else if (*patch.old != '\0') {
237 err = got_path_strip(&p->old, patch.old, strip);
242 if (*patch.new != '\0') {
243 err = got_path_strip(&p->new, patch.new, strip);
248 if (p->old == NULL && p->new == NULL) {
249 err = got_error(GOT_ERR_PATCH_MALFORMED);
258 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
264 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
265 switch (imsg.hdr.type) {
266 case GOT_IMSG_PATCH_DONE:
267 if (h != NULL && h->len == 0)
268 err = got_error(GOT_ERR_PATCH_MALFORMED);
270 case GOT_IMSG_PATCH_HUNK:
272 (h->len == 0 || h->old_nonl || h->new_nonl)) {
273 err = got_error(GOT_ERR_PATCH_MALFORMED);
277 if (datalen != sizeof(hdr)) {
278 err = got_error(GOT_ERR_PRIVSEP_LEN);
281 memcpy(&hdr, imsg.data, sizeof(hdr));
282 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
283 err = got_error(GOT_ERR_PRIVSEP_LEN);
286 if ((h = calloc(1, sizeof(*h))) == NULL) {
287 err = got_error_from_errno("calloc");
290 h->old_from = hdr.oldfrom;
291 h->old_lines = hdr.oldlines;
292 h->new_from = hdr.newfrom;
293 h->new_lines = hdr.newlines;
294 STAILQ_INSERT_TAIL(&p->head, h, entries);
296 case GOT_IMSG_PATCH_LINE:
298 err = got_error(GOT_ERR_PRIVSEP_MSG);
302 /* at least one char */
303 if (datalen < 2 || t[datalen-1] != '\0') {
304 err = got_error(GOT_ERR_PRIVSEP_MSG);
307 if (*t != ' ' && *t != '-' && *t != '+' &&
309 err = got_error(GOT_ERR_PRIVSEP_MSG);
314 err = pushline(h, t, datalen);
315 else if (lastmode == '-')
317 else if (lastmode == '+')
320 err = got_error(GOT_ERR_PATCH_MALFORMED);
328 err = got_error(GOT_ERR_PRIVSEP_MSG);
344 reverse_patch(struct got_patch *p)
346 struct got_patch_hunk *h;
350 STAILQ_FOREACH(h, &p->head, entries) {
352 h->old_from = h->new_from;
356 h->old_lines = h->new_lines;
360 h->old_nonl = h->new_nonl;
363 for (i = 0; i < h->len; ++i) {
364 if (h->lines[i].mode == '+')
365 h->lines[i].mode = '-';
366 else if (h->lines[i].mode == '-')
367 h->lines[i].mode = '+';
373 * Copy data from orig starting at copypos until pos into tmp.
374 * If pos is -1, copy until EOF.
376 static const struct got_error *
377 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
382 if (fseeko(orig, copypos, SEEK_SET) == -1)
383 return got_error_from_errno("fseeko");
385 while (pos == -1 || copypos < pos) {
388 len = MIN(len, (size_t)pos - copypos);
389 r = fread(buf, 1, len, orig);
390 if (r != len && ferror(orig))
391 return got_error_from_errno("fread");
392 w = fwrite(buf, 1, r, tmp);
394 return got_error_from_errno("fwrite");
396 if (r != len && feof(orig)) {
399 return got_error(GOT_ERR_HUNK_FAILED);
405 static int lines_eq(struct got_patch_line *, const char *, size_t, int *);
407 static const struct got_error *
408 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
410 const struct got_error *err = NULL;
411 struct got_patch_line *l = &h->lines[0];
417 int mangled = 0, match_lineno = -1;
421 linelen = getline(&line, &linesize, orig);
424 err = got_error_from_errno("getline");
425 /* An EOF is fine iff the target file is empty. */
426 if (feof(orig) && match == -1 && h->old_lines != 0)
427 err = got_error(GOT_ERR_HUNK_FAILED);
429 match_lineno = (*lineno)-1;
433 if ((mode == ' ' && lines_eq(l, line, linelen, &mangled)) ||
434 (mode == '-' && lines_eq(l, line, linelen, &mangled)) ||
435 (mode == '+' && *lineno == h->old_from)) {
436 match = ftello(orig);
438 err = got_error_from_errno("ftello");
442 match_lineno = (*lineno)-1;
445 if (*lineno >= h->old_from && match != -1) {
454 *lineno = match_lineno;
455 if (fseeko(orig, match, SEEK_SET) == -1)
456 err = got_error_from_errno("fseeko");
464 lines_eq(struct got_patch_line *l, const char *b, size_t len, int *mangled)
469 if (len > 00 && b[len - 1] == '\n')
473 if (l->len == len && !memcmp(a, b, len))
481 (a[i] == '\t' || a[i] == ' ' || a[i] == '\f'))
484 (b[j] == '\t' || b[j] == ' ' || b[j] == '\f'))
486 if (i == l->len || j == len || a[i] != b[j])
491 return (i == l->len && j == len);
494 static const struct got_error *
495 test_hunk(FILE *orig, struct got_patch_hunk *h)
497 const struct got_error *err = NULL;
499 size_t linesize = 0, i = 0;
503 for (i = 0; i < h->len; ++i) {
504 switch (h->lines[i].mode) {
509 linelen = getline(&line, &linesize, orig);
512 err = got_error_from_errno("getline");
515 GOT_ERR_HUNK_FAILED);
518 if (!lines_eq(&h->lines[i], line, linelen, &mangled)) {
519 err = got_error(GOT_ERR_HUNK_FAILED);
533 static const struct got_error *
534 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
537 const struct got_error *err = NULL;
539 size_t linesize = 0, i, new = 0;
545 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
546 return got_error_from_errno("fseeko");
548 for (i = 0; i < h->len; ++i) {
549 switch (mode = h->lines[i].mode) {
554 linelen = getline(&line, &linesize, orig);
556 err = got_error_from_errno("getline");
559 if (line[linelen - 1] == '\n')
560 line[linelen - 1] = '\0';
564 t = h->lines[i].line;
569 if (fwrite(t, 1, l, tmp) != l ||
570 fputc('\n', tmp) == EOF) {
571 err = got_error_from_errno("fprintf");
577 t = h->lines[i].line;
579 if (fwrite(t, 1, l, tmp) != l) {
580 err = got_error_from_errno("fprintf");
583 if (new != h->new_lines || !h->new_nonl) {
584 if (fprintf(tmp, "\n") < 0) {
585 err = got_error_from_errno("fprintf");
598 static const struct got_error *
599 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
601 const struct got_error *err = NULL;
602 struct got_patch_hunk *h;
610 if (p->old == NULL) { /* create */
611 h = STAILQ_FIRST(&p->head);
612 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
613 return got_error(GOT_ERR_PATCH_MALFORMED);
614 return apply_hunk(orig, tmp, h, &lineno, 0);
617 /* When deleting binary files there are no hunks to apply. */
618 if (p->new == NULL && STAILQ_EMPTY(&p->head))
621 if (fstat(fileno(orig), &sb) == -1)
622 return got_error_from_errno("fstat");
625 STAILQ_FOREACH(h, &p->head, entries) {
627 err = locate_hunk(orig, h, &pos, &lineno);
628 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
632 err = copy(tmp, orig, copypos, pos);
637 err = test_hunk(orig, h);
638 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
640 * try to apply the hunk again starting the search
641 * after the previous partial match.
643 if (fseeko(orig, pos, SEEK_SET) == -1)
644 return got_error_from_errno("fseeko");
645 linelen = getline(&line, &linesize, orig);
647 return got_error_from_errno("getline");
654 if (lineno + 1 != h->old_from)
655 h->offset = lineno + 1 - h->old_from;
657 err = apply_hunk(orig, tmp, h, &lineno, pos);
661 copypos = ftello(orig);
663 return got_error_from_errno("ftello");
666 if (p->new == NULL && sb.st_size != copypos) {
667 h = STAILQ_FIRST(&p->head);
668 h->err = got_error(GOT_ERR_HUNK_FAILED);
670 } else if (!feof(orig))
671 err = copy(tmp, orig, copypos, -1);
676 static const struct got_error *
677 report_progress(struct patch_args *pa, const char *old, const char *new,
678 unsigned char status, const struct got_error *orig_error)
680 const struct got_error *err;
681 struct got_patch_hunk *h;
683 err = pa->progress_cb(pa->progress_arg, old, new, status,
684 orig_error, 0, 0, 0, 0, 0, 0, NULL);
688 STAILQ_FOREACH(h, pa->head, entries) {
689 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
692 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
693 h->old_from, h->old_lines, h->new_from, h->new_lines,
694 h->offset, h->ws_mangled, h->err);
702 static const struct got_error *
703 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
706 return report_progress(arg, path, NULL, status, NULL);
709 static const struct got_error *
710 patch_add(void *arg, unsigned char status, const char *path)
712 return report_progress(arg, NULL, path, status, NULL);
715 static const struct got_error *
716 open_blob(char **path, FILE **fp, const char *blobid,
717 struct got_repository *repo)
719 const struct got_error *err = NULL;
720 struct got_blob_object *blob = NULL;
721 struct got_object_id id, *idptr, *matched_id = NULL;
727 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
728 err = got_repo_match_object_id(&matched_id, NULL, blobid,
729 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
735 if (!got_parse_object_id(&id, blobid, GOT_HASH_SHA1))
736 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
740 fd = got_opentempfd();
742 err = got_error_from_errno("got_opentempfd");
746 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
750 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
755 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
760 if (fd != -1 && close(fd) == -1 && err == NULL)
761 err = got_error_from_errno("close");
763 got_object_blob_close(blob);
764 if (matched_id != NULL)
778 static const struct got_error *
779 prepare_merge(int *do_merge, char **apath, FILE **afile,
780 struct got_worktree *worktree, struct got_repository *repo,
781 struct got_patch *p, struct got_object_id *commit_id,
782 struct got_tree_object *tree, const char *path)
784 const struct got_error *err = NULL;
790 /* don't run the diff3 merge on creations/deletions */
791 if (p->old == NULL || p->new == NULL)
795 struct got_object_id *id;
797 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
800 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
801 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
803 err = open_blob(apath, afile, p->blob, repo);
804 *do_merge = err == NULL;
805 } else if (*p->blob != '\0') {
806 err = open_blob(apath, afile, p->blob, repo);
808 * ignore failures to open this blob, we might have
811 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
812 err->code != GOT_ERR_NO_OBJ)
814 *do_merge = err == NULL;
821 static const struct got_error *
822 apply_patch(int *overlapcnt, struct got_worktree *worktree,
823 struct got_repository *repo, struct got_fileindex *fileindex,
824 const char *old, const char *new, struct got_patch *p, int nop,
825 int reverse, struct got_object_id *commit_id,
826 struct got_tree_object *tree, struct patch_args *pa,
827 got_cancel_cb cancel_cb, void *cancel_arg)
829 const struct got_error *err = NULL;
831 int do_merge = 0, file_renamed = 0;
832 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
833 char *oldpath = NULL, *newpath = NULL;
834 char *tmppath = NULL, *template = NULL;
835 char *apath = NULL, *mergepath = NULL;
836 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
838 mode_t mode = GOT_DEFAULT_FILE_MODE;
842 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
843 commit_id, tree, old);
847 if (reverse && !do_merge)
850 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
852 err = got_error_from_errno("asprintf");
856 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
858 err = got_error_from_errno("asprintf");
862 file_renamed = strcmp(oldpath, newpath);
864 if (asprintf(&template, "%s/got-patch",
865 got_worktree_get_root_path(worktree)) == -1) {
866 err = got_error_from_errno(template);
870 if (p->old != NULL) {
871 if ((oldfile = fopen(oldpath, "r")) == NULL) {
872 err = got_error_from_errno2("open", oldpath);
875 if (fstat(fileno(oldfile), &sb) == -1) {
876 err = got_error_from_errno2("fstat", oldpath);
881 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
883 err = got_opentemp_named(&tmppath, &tmpfile, template, "");
886 outfd = fileno(tmpfile);
887 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
892 const char *type, *id;
894 if (fseeko(afile, 0, SEEK_SET) == -1 ||
895 fseeko(oldfile, 0, SEEK_SET) == -1 ||
896 fseeko(tmpfile, 0, SEEK_SET) == -1) {
897 err = got_error_from_errno("fseeko");
901 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
902 err = got_error_from_errno("asprintf");
907 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
908 err = got_error_from_errno("asprintf");
913 if (*p->cid != '\0') {
921 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
922 err = got_error_from_errno("asprintf");
940 err = got_opentemp_named(&mergepath, &mergefile, template, "");
943 outfd = fileno(mergefile);
945 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
946 oldfile, oldlabel, anclabel, newlabel,
947 GOT_DIFF_ALGORITHM_PATIENCE);
955 if (p->old != NULL && p->new == NULL) {
956 err = got_worktree_patch_schedule_rm(old, repo, worktree,
957 fileindex, patch_delete, pa);
961 if (fchmod(outfd, apply_umask(mode)) == -1) {
962 err = got_error_from_errno2("chmod", tmppath);
967 err = got_path_move_file(mergepath, newpath);
973 err = got_path_move_file(tmppath, newpath);
981 err = got_worktree_patch_schedule_rm(old, repo, worktree,
982 fileindex, patch_delete, pa);
984 err = got_worktree_patch_schedule_add(new, repo,
985 worktree, fileindex, patch_add,
989 } else if (p->old == NULL) {
990 err = got_worktree_patch_schedule_add(new, repo, worktree,
991 fileindex, patch_add, pa);
994 } else if (*overlapcnt != 0)
995 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
997 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
999 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
1004 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1005 err = got_error_from_errno("unlink");
1006 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
1007 err = got_error_from_errno("fclose");
1011 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
1012 err = got_error_from_errno("fclose");
1014 if (apath != NULL && unlink(apath) == -1 && err == NULL)
1015 err = got_error_from_errno("unlink");
1016 if (afile != NULL && fclose(afile) == EOF && err == NULL)
1017 err = got_error_from_errno("fclose");
1020 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
1021 err = got_error_from_errno("unlink");
1022 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
1023 err = got_error_from_errno("fclose");
1033 const struct got_error *
1034 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1035 int nop, int strip, int reverse, struct got_object_id *commit_id,
1036 got_patch_progress_cb progress_cb, void *progress_arg,
1037 got_cancel_cb cancel_cb, void *cancel_arg)
1039 const struct got_error *err = NULL, *complete_err = NULL;
1040 struct got_fileindex *fileindex = NULL;
1041 struct got_commit_object *commit = NULL;
1042 struct got_tree_object *tree = NULL;
1043 char *fileindex_path = NULL;
1044 char *oldpath, *newpath;
1045 struct imsgbuf *ibuf;
1046 int imsg_fds[2] = {-1, -1};
1047 int overlapcnt, done = 0, failed = 0;
1050 ibuf = calloc(1, sizeof(*ibuf));
1052 err = got_error_from_errno("calloc");
1056 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1057 err = got_error_from_errno("socketpair");
1063 err = got_error_from_errno("fork");
1065 } else if (pid == 0) {
1066 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1071 if (close(imsg_fds[1]) == -1) {
1072 err = got_error_from_errno("close");
1076 imsg_init(ibuf, imsg_fds[0]);
1078 err = send_patch(ibuf, fd);
1083 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1089 err = got_object_open_as_commit(&commit, repo, commit_id);
1093 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1098 while (!done && err == NULL) {
1100 struct patch_args pa;
1102 pa.progress_cb = progress_cb;
1103 pa.progress_arg = progress_arg;
1106 err = recv_patch(ibuf, &done, &p, strip);
1110 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1111 &newpath, worktree, repo, fileindex);
1113 err = apply_patch(&overlapcnt, worktree, repo,
1114 fileindex, oldpath, newpath, &p, nop, reverse,
1115 commit_id, tree, &pa, cancel_cb, cancel_arg);
1118 /* recoverable errors */
1119 if (err->code == GOT_ERR_FILE_STATUS ||
1120 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1121 err = report_progress(&pa, p.old, p.new,
1122 GOT_STATUS_CANNOT_UPDATE, err);
1123 else if (err->code == GOT_ERR_HUNK_FAILED)
1124 err = report_progress(&pa, p.old, p.new,
1125 GOT_STATUS_CANNOT_UPDATE, NULL);
1127 if (overlapcnt != 0)
1139 if (fileindex != NULL)
1140 complete_err = got_worktree_patch_complete(fileindex,
1142 if (complete_err && err == NULL)
1144 free(fileindex_path);
1146 got_object_tree_close(tree);
1148 got_object_commit_close(commit);
1149 if (fd != -1 && close(fd) == -1 && err == NULL)
1150 err = got_error_from_errno("close");
1153 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1154 err = got_error_from_errno("close");
1155 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1156 err = got_error_from_errno("close");
1157 if (err == NULL && failed)
1158 err = got_error(GOT_ERR_PATCH_FAILED);