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>
40 #include "got_error.h"
41 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_repository.h"
47 #include "got_opentemp.h"
48 #include "got_patch.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_diff.h"
53 #include "got_lib_object.h"
54 #include "got_lib_privsep.h"
55 #include "got_lib_sha1.h"
57 #define MIN(a, b) ((a) < (b) ? (a) : (b))
59 struct got_patch_hunk {
60 STAILQ_ENTRY(got_patch_hunk) entries;
61 const struct got_error *err;
75 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
82 struct got_patch_hunk_head head;
86 got_patch_progress_cb progress_cb;
88 struct got_patch_hunk_head *head;
92 apply_umask(mode_t mode)
101 static const struct got_error *
102 send_patch(struct imsgbuf *ibuf, int fd)
104 const struct got_error *err = NULL;
106 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
108 err = got_error_from_errno(
109 "imsg_compose GOT_IMSG_PATCH_FILE");
114 return got_privsep_flush_imsg(ibuf);
118 patch_free(struct got_patch *p)
120 struct got_patch_hunk *h;
123 while (!STAILQ_EMPTY(&p->head)) {
124 h = STAILQ_FIRST(&p->head);
125 STAILQ_REMOVE_HEAD(&p->head, entries);
127 for (i = 0; i < h->len; ++i)
136 memset(p, 0, sizeof(*p));
137 STAILQ_INIT(&p->head);
140 static const struct got_error *
141 pushline(struct got_patch_hunk *h, const char *line)
146 if (h->len == h->cap) {
147 if ((newcap = h->cap * 1.5) == 0)
149 t = recallocarray(h->lines, h->cap, newcap,
150 sizeof(h->lines[0]));
152 return got_error_from_errno("recallocarray");
157 if ((t = strdup(line)) == NULL)
158 return got_error_from_errno("strdup");
160 h->lines[h->len++] = t;
164 static const struct got_error *
165 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
167 const struct got_error *err = NULL;
169 struct got_imsg_patch_hunk hdr;
170 struct got_imsg_patch patch;
171 struct got_patch_hunk *h = NULL;
175 memset(p, 0, sizeof(*p));
176 STAILQ_INIT(&p->head);
178 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
181 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
185 if (imsg.hdr.type != GOT_IMSG_PATCH) {
186 err = got_error(GOT_ERR_PRIVSEP_MSG);
189 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
190 if (datalen != sizeof(patch)) {
191 err = got_error(GOT_ERR_PRIVSEP_LEN);
194 memcpy(&patch, imsg.data, sizeof(patch));
196 if (patch.old[sizeof(patch.old)-1] != '\0' ||
197 patch.new[sizeof(patch.new)-1] != '\0' ||
198 patch.cid[sizeof(patch.cid)-1] != '\0' ||
199 patch.blob[sizeof(patch.blob)-1] != '\0') {
200 err = got_error(GOT_ERR_PRIVSEP_LEN);
204 if (*patch.cid != '\0')
205 strlcpy(p->cid, patch.cid, sizeof(p->cid));
207 if (*patch.blob != '\0')
208 strlcpy(p->blob, patch.blob, sizeof(p->blob));
210 p->xbit = patch.xbit;
212 /* automatically set strip=1 for git-style diffs */
213 if (strip == -1 && patch.git &&
214 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
215 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
218 /* prefer the new name if not /dev/null for not git-style diffs */
219 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
220 err = got_path_strip(&p->old, patch.new, strip);
223 } else if (*patch.old != '\0') {
224 err = got_path_strip(&p->old, patch.old, strip);
229 if (*patch.new != '\0') {
230 err = got_path_strip(&p->new, patch.new, strip);
235 if (p->old == NULL && p->new == NULL) {
236 err = got_error(GOT_ERR_PATCH_MALFORMED);
245 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
251 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
252 switch (imsg.hdr.type) {
253 case GOT_IMSG_PATCH_DONE:
254 if (h != NULL && h->len == 0)
255 err = got_error(GOT_ERR_PATCH_MALFORMED);
257 case GOT_IMSG_PATCH_HUNK:
259 (h->len == 0 || h->old_nonl || h->new_nonl)) {
260 err = got_error(GOT_ERR_PATCH_MALFORMED);
264 if (datalen != sizeof(hdr)) {
265 err = got_error(GOT_ERR_PRIVSEP_LEN);
268 memcpy(&hdr, imsg.data, sizeof(hdr));
269 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
270 err = got_error(GOT_ERR_PRIVSEP_LEN);
273 if ((h = calloc(1, sizeof(*h))) == NULL) {
274 err = got_error_from_errno("calloc");
277 h->old_from = hdr.oldfrom;
278 h->old_lines = hdr.oldlines;
279 h->new_from = hdr.newfrom;
280 h->new_lines = hdr.newlines;
281 STAILQ_INSERT_TAIL(&p->head, h, entries);
283 case GOT_IMSG_PATCH_LINE:
285 err = got_error(GOT_ERR_PRIVSEP_MSG);
289 /* at least one char */
290 if (datalen < 2 || t[datalen-1] != '\0') {
291 err = got_error(GOT_ERR_PRIVSEP_MSG);
294 if (*t != ' ' && *t != '-' && *t != '+' &&
296 err = got_error(GOT_ERR_PRIVSEP_MSG);
301 err = pushline(h, t);
302 else if (lastmode == '-')
304 else if (lastmode == '+')
307 err = got_error(GOT_ERR_PATCH_MALFORMED);
315 err = got_error(GOT_ERR_PRIVSEP_MSG);
331 reverse_patch(struct got_patch *p)
333 struct got_patch_hunk *h;
337 STAILQ_FOREACH(h, &p->head, entries) {
339 h->old_from = h->new_from;
343 h->old_lines = h->new_lines;
347 h->old_nonl = h->new_nonl;
350 for (i = 0; i < h->len; ++i) {
351 if (*h->lines[i] == '+')
353 else if (*h->lines[i] == '-')
360 * Copy data from orig starting at copypos until pos into tmp.
361 * If pos is -1, copy until EOF.
363 static const struct got_error *
364 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
369 if (fseeko(orig, copypos, SEEK_SET) == -1)
370 return got_error_from_errno("fseeko");
372 while (pos == -1 || copypos < pos) {
375 len = MIN(len, (size_t)pos - copypos);
376 r = fread(buf, 1, len, orig);
377 if (r != len && ferror(orig))
378 return got_error_from_errno("fread");
379 w = fwrite(buf, 1, r, tmp);
381 return got_error_from_errno("fwrite");
383 if (r != len && feof(orig)) {
386 return got_error(GOT_ERR_HUNK_FAILED);
392 static int linecmp(const char *, const char *, int *);
394 static const struct got_error *
395 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
397 const struct got_error *err = NULL;
399 char mode = *h->lines[0];
403 int mangled = 0, match_lineno = -1;
406 linelen = getline(&line, &linesize, orig);
409 err = got_error_from_errno("getline");
410 else if (match == -1)
411 err = got_error(GOT_ERR_HUNK_FAILED);
414 if (line[linelen - 1] == '\n')
415 line[linelen - 1] = '\0';
418 if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
419 (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
420 (mode == '+' && *lineno == h->old_from)) {
421 match = ftello(orig);
423 err = got_error_from_errno("ftello");
427 match_lineno = (*lineno)-1;
430 if (*lineno >= h->old_from && match != -1) {
439 *lineno = match_lineno;
440 if (fseeko(orig, match, SEEK_SET) == -1)
441 err = got_error_from_errno("fseeko");
449 linecmp(const char *a, const char *b, int *mangled)
460 while (*a == '\t' || *a == ' ' || *a == '\f')
462 while (*b == '\t' || *b == ' ' || *b == '\f')
464 if (*a == '\0' || *a != *b)
472 static const struct got_error *
473 test_hunk(FILE *orig, struct got_patch_hunk *h)
475 const struct got_error *err = NULL;
477 size_t linesize = 0, i = 0;
481 for (i = 0; i < h->len; ++i) {
482 switch (*h->lines[i]) {
487 linelen = getline(&line, &linesize, orig);
490 err = got_error_from_errno("getline");
493 GOT_ERR_HUNK_FAILED);
496 if (line[linelen - 1] == '\n')
497 line[linelen - 1] = '\0';
498 if (linecmp(h->lines[i] + 1, line, &mangled)) {
499 err = got_error(GOT_ERR_HUNK_FAILED);
513 static const struct got_error *
514 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
517 const struct got_error *err = NULL;
519 size_t linesize = 0, i, new = 0;
524 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
525 return got_error_from_errno("fseeko");
527 for (i = 0; i < h->len; ++i) {
528 switch (mode = *h->lines[i]) {
533 linelen = getline(&line, &linesize, orig);
535 err = got_error_from_errno("getline");
538 if (line[linelen - 1] == '\n')
539 line[linelen - 1] = '\0';
545 if (fprintf(tmp, "%s\n", t) < 0) {
546 err = got_error_from_errno("fprintf");
552 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
553 err = got_error_from_errno("fprintf");
556 if (new != h->new_lines || !h->new_nonl) {
557 if (fprintf(tmp, "\n") < 0) {
558 err = got_error_from_errno("fprintf");
571 static const struct got_error *
572 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
574 const struct got_error *err = NULL;
575 struct got_patch_hunk *h;
583 if (p->old == NULL) { /* create */
584 h = STAILQ_FIRST(&p->head);
585 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
586 return got_error(GOT_ERR_PATCH_MALFORMED);
587 return apply_hunk(orig, tmp, h, &lineno, 0);
590 /* When deleting binary files there are no hunks to apply. */
591 if (p->new == NULL && STAILQ_EMPTY(&p->head))
594 if (fstat(fileno(orig), &sb) == -1)
595 return got_error_from_errno("fstat");
598 STAILQ_FOREACH(h, &p->head, entries) {
600 err = locate_hunk(orig, h, &pos, &lineno);
601 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
605 err = copy(tmp, orig, copypos, pos);
610 err = test_hunk(orig, h);
611 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
613 * try to apply the hunk again starting the search
614 * after the previous partial match.
616 if (fseeko(orig, pos, SEEK_SET) == -1)
617 return got_error_from_errno("fseeko");
618 linelen = getline(&line, &linesize, orig);
620 return got_error_from_errno("getline");
627 if (lineno + 1 != h->old_from)
628 h->offset = lineno + 1 - h->old_from;
630 err = apply_hunk(orig, tmp, h, &lineno, pos);
634 copypos = ftello(orig);
636 return got_error_from_errno("ftello");
639 if (p->new == NULL && sb.st_size != copypos) {
640 h = STAILQ_FIRST(&p->head);
641 h->err = got_error(GOT_ERR_HUNK_FAILED);
643 } else if (!feof(orig))
644 err = copy(tmp, orig, copypos, -1);
649 static const struct got_error *
650 report_progress(struct patch_args *pa, const char *old, const char *new,
651 unsigned char status, const struct got_error *orig_error)
653 const struct got_error *err;
654 struct got_patch_hunk *h;
656 err = pa->progress_cb(pa->progress_arg, old, new, status,
657 orig_error, 0, 0, 0, 0, 0, 0, NULL);
661 STAILQ_FOREACH(h, pa->head, entries) {
662 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
665 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
666 h->old_from, h->old_lines, h->new_from, h->new_lines,
667 h->offset, h->ws_mangled, h->err);
675 static const struct got_error *
676 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
679 return report_progress(arg, path, NULL, status, NULL);
682 static const struct got_error *
683 patch_add(void *arg, unsigned char status, const char *path)
685 return report_progress(arg, NULL, path, status, NULL);
688 static const struct got_error *
689 open_blob(char **path, FILE **fp, const char *blobid,
690 struct got_repository *repo)
692 const struct got_error *err = NULL;
693 struct got_blob_object *blob = NULL;
694 struct got_object_id id, *idptr, *matched_id = NULL;
700 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
701 err = got_repo_match_object_id(&matched_id, NULL, blobid,
702 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
708 if (!got_parse_sha1_digest(id.sha1, blobid))
709 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
713 fd = got_opentempfd();
715 err = got_error_from_errno("got_opentempfd");
719 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
723 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
728 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
733 if (fd != -1 && close(fd) == -1 && err == NULL)
734 err = got_error_from_errno("close");
736 got_object_blob_close(blob);
737 if (matched_id != NULL)
751 static const struct got_error *
752 prepare_merge(int *do_merge, char **apath, FILE **afile,
753 struct got_worktree *worktree, struct got_repository *repo,
754 struct got_patch *p, struct got_object_id *commit_id,
755 struct got_tree_object *tree, const char *path)
757 const struct got_error *err = NULL;
763 /* don't run the diff3 merge on creations/deletions */
764 if (p->old == NULL || p->new == NULL)
768 struct got_object_id *id;
770 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
773 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
774 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
776 err = open_blob(apath, afile, p->blob, repo);
777 *do_merge = err == NULL;
778 } else if (*p->blob != '\0') {
779 err = open_blob(apath, afile, p->blob, repo);
781 * ignore failures to open this blob, we might have
784 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
785 err->code != GOT_ERR_NO_OBJ)
787 *do_merge = err == NULL;
794 static const struct got_error *
795 apply_patch(int *overlapcnt, struct got_worktree *worktree,
796 struct got_repository *repo, struct got_fileindex *fileindex,
797 const char *old, const char *new, struct got_patch *p, int nop,
798 int reverse, struct got_object_id *commit_id,
799 struct got_tree_object *tree, struct patch_args *pa,
800 got_cancel_cb cancel_cb, void *cancel_arg)
802 const struct got_error *err = NULL;
804 int do_merge = 0, file_renamed = 0;
805 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
806 char *oldpath = NULL, *newpath = NULL;
807 char *tmppath = NULL, *template = NULL;
808 char *apath = NULL, *mergepath = NULL;
809 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
811 mode_t mode = GOT_DEFAULT_FILE_MODE;
815 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
816 commit_id, tree, old);
820 if (reverse && !do_merge)
823 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
825 err = got_error_from_errno("asprintf");
829 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
831 err = got_error_from_errno("asprintf");
835 file_renamed = strcmp(oldpath, newpath);
837 if (asprintf(&template, "%s/got-patch",
838 got_worktree_get_root_path(worktree)) == -1) {
839 err = got_error_from_errno(template);
843 if (p->old != NULL) {
844 if ((oldfile = fopen(oldpath, "r")) == NULL) {
845 err = got_error_from_errno2("open", oldpath);
848 if (fstat(fileno(oldfile), &sb) == -1) {
849 err = got_error_from_errno2("fstat", oldpath);
854 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
856 err = got_opentemp_named(&tmppath, &tmpfile, template, "");
859 outfd = fileno(tmpfile);
860 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
865 const char *type, *id;
867 if (fseeko(afile, 0, SEEK_SET) == -1 ||
868 fseeko(oldfile, 0, SEEK_SET) == -1 ||
869 fseeko(tmpfile, 0, SEEK_SET) == -1) {
870 err = got_error_from_errno("fseeko");
874 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
875 err = got_error_from_errno("asprintf");
880 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
881 err = got_error_from_errno("asprintf");
886 if (*p->cid != '\0') {
894 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
895 err = got_error_from_errno("asprintf");
913 err = got_opentemp_named(&mergepath, &mergefile, template, "");
916 outfd = fileno(mergefile);
918 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
919 oldfile, oldlabel, anclabel, newlabel,
920 GOT_DIFF_ALGORITHM_PATIENCE);
928 if (p->old != NULL && p->new == NULL) {
929 err = got_worktree_patch_schedule_rm(old, repo, worktree,
930 fileindex, patch_delete, pa);
934 if (fchmod(outfd, apply_umask(mode)) == -1) {
935 err = got_error_from_errno2("chmod", tmppath);
940 err = got_path_move_file(mergepath, newpath);
946 err = got_path_move_file(tmppath, newpath);
954 err = got_worktree_patch_schedule_rm(old, repo, worktree,
955 fileindex, patch_delete, pa);
957 err = got_worktree_patch_schedule_add(new, repo,
958 worktree, fileindex, patch_add,
962 } else if (p->old == NULL) {
963 err = got_worktree_patch_schedule_add(new, repo, worktree,
964 fileindex, patch_add, pa);
967 } else if (*overlapcnt != 0)
968 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
970 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
972 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
977 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
978 err = got_error_from_errno("unlink");
979 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
980 err = got_error_from_errno("fclose");
984 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
985 err = got_error_from_errno("fclose");
987 if (apath != NULL && unlink(apath) == -1 && err == NULL)
988 err = got_error_from_errno("unlink");
989 if (afile != NULL && fclose(afile) == EOF && err == NULL)
990 err = got_error_from_errno("fclose");
993 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
994 err = got_error_from_errno("unlink");
995 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
996 err = got_error_from_errno("fclose");
1006 const struct got_error *
1007 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1008 int nop, int strip, int reverse, struct got_object_id *commit_id,
1009 got_patch_progress_cb progress_cb, void *progress_arg,
1010 got_cancel_cb cancel_cb, void *cancel_arg)
1012 const struct got_error *err = NULL, *complete_err = NULL;
1013 struct got_fileindex *fileindex = NULL;
1014 struct got_commit_object *commit = NULL;
1015 struct got_tree_object *tree = NULL;
1016 char *fileindex_path = NULL;
1017 char *oldpath, *newpath;
1018 struct imsgbuf *ibuf;
1019 int imsg_fds[2] = {-1, -1};
1020 int overlapcnt, done = 0, failed = 0;
1023 ibuf = calloc(1, sizeof(*ibuf));
1025 err = got_error_from_errno("calloc");
1029 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1030 err = got_error_from_errno("socketpair");
1036 err = got_error_from_errno("fork");
1038 } else if (pid == 0) {
1039 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1044 if (close(imsg_fds[1]) == -1) {
1045 err = got_error_from_errno("close");
1049 imsg_init(ibuf, imsg_fds[0]);
1051 err = send_patch(ibuf, fd);
1056 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1062 err = got_object_open_as_commit(&commit, repo, commit_id);
1066 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1071 while (!done && err == NULL) {
1073 struct patch_args pa;
1075 pa.progress_cb = progress_cb;
1076 pa.progress_arg = progress_arg;
1079 err = recv_patch(ibuf, &done, &p, strip);
1083 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1084 &newpath, worktree, repo, fileindex);
1086 err = apply_patch(&overlapcnt, worktree, repo,
1087 fileindex, oldpath, newpath, &p, nop, reverse,
1088 commit_id, tree, &pa, cancel_cb, cancel_arg);
1091 /* recoverable errors */
1092 if (err->code == GOT_ERR_FILE_STATUS ||
1093 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1094 err = report_progress(&pa, p.old, p.new,
1095 GOT_STATUS_CANNOT_UPDATE, err);
1096 else if (err->code == GOT_ERR_HUNK_FAILED)
1097 err = report_progress(&pa, p.old, p.new,
1098 GOT_STATUS_CANNOT_UPDATE, NULL);
1100 if (overlapcnt != 0)
1112 if (fileindex != NULL)
1113 complete_err = got_worktree_patch_complete(fileindex,
1115 if (complete_err && err == NULL)
1117 free(fileindex_path);
1119 got_object_tree_close(tree);
1121 got_object_commit_close(commit);
1122 if (fd != -1 && close(fd) == -1 && err == NULL)
1123 err = got_error_from_errno("close");
1126 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1127 err = got_error_from_errno("close");
1128 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1129 err = got_error_from_errno("close");
1130 if (err == NULL && failed)
1131 err = got_error(GOT_ERR_PATCH_FAILED);