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;
420 linelen = getline(&line, &linesize, orig);
423 err = got_error_from_errno("getline");
424 else if (match == -1)
425 err = got_error(GOT_ERR_HUNK_FAILED);
430 if ((mode == ' ' && lines_eq(l, line, linelen, &mangled)) ||
431 (mode == '-' && lines_eq(l, line, linelen, &mangled)) ||
432 (mode == '+' && *lineno == h->old_from)) {
433 match = ftello(orig);
435 err = got_error_from_errno("ftello");
439 match_lineno = (*lineno)-1;
442 if (*lineno >= h->old_from && match != -1) {
451 *lineno = match_lineno;
452 if (fseeko(orig, match, SEEK_SET) == -1)
453 err = got_error_from_errno("fseeko");
461 lines_eq(struct got_patch_line *l, const char *b, size_t len, int *mangled)
466 if (len > 00 && b[len - 1] == '\n')
470 if (l->len == len && !memcmp(a, b, len))
478 (a[i] == '\t' || a[i] == ' ' || a[i] == '\f'))
481 (b[j] == '\t' || b[j] == ' ' || b[j] == '\f'))
483 if (i == l->len || j == len || a[i] != b[j])
488 return (i == l->len && j == len);
491 static const struct got_error *
492 test_hunk(FILE *orig, struct got_patch_hunk *h)
494 const struct got_error *err = NULL;
496 size_t linesize = 0, i = 0;
500 for (i = 0; i < h->len; ++i) {
501 switch (h->lines[i].mode) {
506 linelen = getline(&line, &linesize, orig);
509 err = got_error_from_errno("getline");
512 GOT_ERR_HUNK_FAILED);
515 if (!lines_eq(&h->lines[i], line, linelen, &mangled)) {
516 err = got_error(GOT_ERR_HUNK_FAILED);
530 static const struct got_error *
531 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
534 const struct got_error *err = NULL;
536 size_t linesize = 0, i, new = 0;
542 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
543 return got_error_from_errno("fseeko");
545 for (i = 0; i < h->len; ++i) {
546 switch (mode = h->lines[i].mode) {
551 linelen = getline(&line, &linesize, orig);
553 err = got_error_from_errno("getline");
556 if (line[linelen - 1] == '\n')
557 line[linelen - 1] = '\0';
561 t = h->lines[i].line;
566 if (fwrite(t, 1, l, tmp) != l ||
567 fputc('\n', tmp) == EOF) {
568 err = got_error_from_errno("fprintf");
574 t = h->lines[i].line;
576 if (fwrite(t, 1, l, tmp) != l) {
577 err = got_error_from_errno("fprintf");
580 if (new != h->new_lines || !h->new_nonl) {
581 if (fprintf(tmp, "\n") < 0) {
582 err = got_error_from_errno("fprintf");
595 static const struct got_error *
596 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
598 const struct got_error *err = NULL;
599 struct got_patch_hunk *h;
607 if (p->old == NULL) { /* create */
608 h = STAILQ_FIRST(&p->head);
609 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
610 return got_error(GOT_ERR_PATCH_MALFORMED);
611 return apply_hunk(orig, tmp, h, &lineno, 0);
614 /* When deleting binary files there are no hunks to apply. */
615 if (p->new == NULL && STAILQ_EMPTY(&p->head))
618 if (fstat(fileno(orig), &sb) == -1)
619 return got_error_from_errno("fstat");
622 STAILQ_FOREACH(h, &p->head, entries) {
624 err = locate_hunk(orig, h, &pos, &lineno);
625 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
629 err = copy(tmp, orig, copypos, pos);
634 err = test_hunk(orig, h);
635 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
637 * try to apply the hunk again starting the search
638 * after the previous partial match.
640 if (fseeko(orig, pos, SEEK_SET) == -1)
641 return got_error_from_errno("fseeko");
642 linelen = getline(&line, &linesize, orig);
644 return got_error_from_errno("getline");
651 if (lineno + 1 != h->old_from)
652 h->offset = lineno + 1 - h->old_from;
654 err = apply_hunk(orig, tmp, h, &lineno, pos);
658 copypos = ftello(orig);
660 return got_error_from_errno("ftello");
663 if (p->new == NULL && sb.st_size != copypos) {
664 h = STAILQ_FIRST(&p->head);
665 h->err = got_error(GOT_ERR_HUNK_FAILED);
667 } else if (!feof(orig))
668 err = copy(tmp, orig, copypos, -1);
673 static const struct got_error *
674 report_progress(struct patch_args *pa, const char *old, const char *new,
675 unsigned char status, const struct got_error *orig_error)
677 const struct got_error *err;
678 struct got_patch_hunk *h;
680 err = pa->progress_cb(pa->progress_arg, old, new, status,
681 orig_error, 0, 0, 0, 0, 0, 0, NULL);
685 STAILQ_FOREACH(h, pa->head, entries) {
686 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
689 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
690 h->old_from, h->old_lines, h->new_from, h->new_lines,
691 h->offset, h->ws_mangled, h->err);
699 static const struct got_error *
700 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
703 return report_progress(arg, path, NULL, status, NULL);
706 static const struct got_error *
707 patch_add(void *arg, unsigned char status, const char *path)
709 return report_progress(arg, NULL, path, status, NULL);
712 static const struct got_error *
713 open_blob(char **path, FILE **fp, const char *blobid,
714 struct got_repository *repo)
716 const struct got_error *err = NULL;
717 struct got_blob_object *blob = NULL;
718 struct got_object_id id, *idptr, *matched_id = NULL;
724 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
725 err = got_repo_match_object_id(&matched_id, NULL, blobid,
726 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
732 if (!got_parse_object_id(&id, blobid, GOT_HASH_SHA1))
733 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
737 fd = got_opentempfd();
739 err = got_error_from_errno("got_opentempfd");
743 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
747 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
752 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
757 if (fd != -1 && close(fd) == -1 && err == NULL)
758 err = got_error_from_errno("close");
760 got_object_blob_close(blob);
761 if (matched_id != NULL)
775 static const struct got_error *
776 prepare_merge(int *do_merge, char **apath, FILE **afile,
777 struct got_worktree *worktree, struct got_repository *repo,
778 struct got_patch *p, struct got_object_id *commit_id,
779 struct got_tree_object *tree, const char *path)
781 const struct got_error *err = NULL;
787 /* don't run the diff3 merge on creations/deletions */
788 if (p->old == NULL || p->new == NULL)
792 struct got_object_id *id;
794 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
797 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
798 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
800 err = open_blob(apath, afile, p->blob, repo);
801 *do_merge = err == NULL;
802 } else if (*p->blob != '\0') {
803 err = open_blob(apath, afile, p->blob, repo);
805 * ignore failures to open this blob, we might have
808 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
809 err->code != GOT_ERR_NO_OBJ)
811 *do_merge = err == NULL;
818 static const struct got_error *
819 apply_patch(int *overlapcnt, struct got_worktree *worktree,
820 struct got_repository *repo, struct got_fileindex *fileindex,
821 const char *old, const char *new, struct got_patch *p, int nop,
822 int reverse, struct got_object_id *commit_id,
823 struct got_tree_object *tree, struct patch_args *pa,
824 got_cancel_cb cancel_cb, void *cancel_arg)
826 const struct got_error *err = NULL;
828 int do_merge = 0, file_renamed = 0;
829 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
830 char *oldpath = NULL, *newpath = NULL;
831 char *tmppath = NULL, *template = NULL;
832 char *apath = NULL, *mergepath = NULL;
833 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
835 mode_t mode = GOT_DEFAULT_FILE_MODE;
839 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
840 commit_id, tree, old);
844 if (reverse && !do_merge)
847 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
849 err = got_error_from_errno("asprintf");
853 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
855 err = got_error_from_errno("asprintf");
859 file_renamed = strcmp(oldpath, newpath);
861 if (asprintf(&template, "%s/got-patch",
862 got_worktree_get_root_path(worktree)) == -1) {
863 err = got_error_from_errno(template);
867 if (p->old != NULL) {
868 if ((oldfile = fopen(oldpath, "r")) == NULL) {
869 err = got_error_from_errno2("open", oldpath);
872 if (fstat(fileno(oldfile), &sb) == -1) {
873 err = got_error_from_errno2("fstat", oldpath);
878 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
880 err = got_opentemp_named(&tmppath, &tmpfile, template, "");
883 outfd = fileno(tmpfile);
884 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
889 const char *type, *id;
891 if (fseeko(afile, 0, SEEK_SET) == -1 ||
892 fseeko(oldfile, 0, SEEK_SET) == -1 ||
893 fseeko(tmpfile, 0, SEEK_SET) == -1) {
894 err = got_error_from_errno("fseeko");
898 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
899 err = got_error_from_errno("asprintf");
904 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
905 err = got_error_from_errno("asprintf");
910 if (*p->cid != '\0') {
918 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
919 err = got_error_from_errno("asprintf");
937 err = got_opentemp_named(&mergepath, &mergefile, template, "");
940 outfd = fileno(mergefile);
942 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
943 oldfile, oldlabel, anclabel, newlabel,
944 GOT_DIFF_ALGORITHM_PATIENCE);
952 if (p->old != NULL && p->new == NULL) {
953 err = got_worktree_patch_schedule_rm(old, repo, worktree,
954 fileindex, patch_delete, pa);
958 if (fchmod(outfd, apply_umask(mode)) == -1) {
959 err = got_error_from_errno2("chmod", tmppath);
964 err = got_path_move_file(mergepath, newpath);
970 err = got_path_move_file(tmppath, newpath);
978 err = got_worktree_patch_schedule_rm(old, repo, worktree,
979 fileindex, patch_delete, pa);
981 err = got_worktree_patch_schedule_add(new, repo,
982 worktree, fileindex, patch_add,
986 } else if (p->old == NULL) {
987 err = got_worktree_patch_schedule_add(new, repo, worktree,
988 fileindex, patch_add, pa);
991 } else if (*overlapcnt != 0)
992 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
994 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
996 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
1001 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1002 err = got_error_from_errno("unlink");
1003 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
1004 err = got_error_from_errno("fclose");
1008 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
1009 err = got_error_from_errno("fclose");
1011 if (apath != NULL && unlink(apath) == -1 && err == NULL)
1012 err = got_error_from_errno("unlink");
1013 if (afile != NULL && fclose(afile) == EOF && err == NULL)
1014 err = got_error_from_errno("fclose");
1017 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
1018 err = got_error_from_errno("unlink");
1019 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
1020 err = got_error_from_errno("fclose");
1030 const struct got_error *
1031 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1032 int nop, int strip, int reverse, struct got_object_id *commit_id,
1033 got_patch_progress_cb progress_cb, void *progress_arg,
1034 got_cancel_cb cancel_cb, void *cancel_arg)
1036 const struct got_error *err = NULL, *complete_err = NULL;
1037 struct got_fileindex *fileindex = NULL;
1038 struct got_commit_object *commit = NULL;
1039 struct got_tree_object *tree = NULL;
1040 char *fileindex_path = NULL;
1041 char *oldpath, *newpath;
1042 struct imsgbuf *ibuf;
1043 int imsg_fds[2] = {-1, -1};
1044 int overlapcnt, done = 0, failed = 0;
1047 ibuf = calloc(1, sizeof(*ibuf));
1049 err = got_error_from_errno("calloc");
1053 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1054 err = got_error_from_errno("socketpair");
1060 err = got_error_from_errno("fork");
1062 } else if (pid == 0) {
1063 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1068 if (close(imsg_fds[1]) == -1) {
1069 err = got_error_from_errno("close");
1073 imsg_init(ibuf, imsg_fds[0]);
1075 err = send_patch(ibuf, fd);
1080 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1086 err = got_object_open_as_commit(&commit, repo, commit_id);
1090 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1095 while (!done && err == NULL) {
1097 struct patch_args pa;
1099 pa.progress_cb = progress_cb;
1100 pa.progress_arg = progress_arg;
1103 err = recv_patch(ibuf, &done, &p, strip);
1107 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1108 &newpath, worktree, repo, fileindex);
1110 err = apply_patch(&overlapcnt, worktree, repo,
1111 fileindex, oldpath, newpath, &p, nop, reverse,
1112 commit_id, tree, &pa, cancel_cb, cancel_arg);
1115 /* recoverable errors */
1116 if (err->code == GOT_ERR_FILE_STATUS ||
1117 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1118 err = report_progress(&pa, p.old, p.new,
1119 GOT_STATUS_CANNOT_UPDATE, err);
1120 else if (err->code == GOT_ERR_HUNK_FAILED)
1121 err = report_progress(&pa, p.old, p.new,
1122 GOT_STATUS_CANNOT_UPDATE, NULL);
1124 if (overlapcnt != 0)
1136 if (fileindex != NULL)
1137 complete_err = got_worktree_patch_complete(fileindex,
1139 if (complete_err && err == NULL)
1141 free(fileindex_path);
1143 got_object_tree_close(tree);
1145 got_object_commit_close(commit);
1146 if (fd != -1 && close(fd) == -1 && err == NULL)
1147 err = got_error_from_errno("close");
1150 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1151 err = got_error_from_errno("close");
1152 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1153 err = got_error_from_errno("close");
1154 if (err == NULL && failed)
1155 err = got_error(GOT_ERR_PATCH_FAILED);