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>
38 #include "got_error.h"
39 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_cancel.h"
43 #include "got_worktree.h"
44 #include "got_repository.h"
45 #include "got_opentemp.h"
46 #include "got_patch.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_diff.h"
51 #include "got_lib_object.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_sha1.h"
55 #define MIN(a, b) ((a) < (b) ? (a) : (b))
57 struct got_patch_hunk {
58 STAILQ_ENTRY(got_patch_hunk) entries;
59 const struct got_error *err;
73 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
80 struct got_patch_hunk_head head;
84 got_patch_progress_cb progress_cb;
86 struct got_patch_hunk_head *head;
90 apply_umask(mode_t mode)
99 static const struct got_error *
100 send_patch(struct imsgbuf *ibuf, int fd)
102 const struct got_error *err = NULL;
104 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
106 err = got_error_from_errno(
107 "imsg_compose GOT_IMSG_PATCH_FILE");
112 return got_privsep_flush_imsg(ibuf);
116 patch_free(struct got_patch *p)
118 struct got_patch_hunk *h;
121 while (!STAILQ_EMPTY(&p->head)) {
122 h = STAILQ_FIRST(&p->head);
123 STAILQ_REMOVE_HEAD(&p->head, entries);
125 for (i = 0; i < h->len; ++i)
134 memset(p, 0, sizeof(*p));
135 STAILQ_INIT(&p->head);
138 static const struct got_error *
139 pushline(struct got_patch_hunk *h, const char *line)
144 if (h->len == h->cap) {
145 if ((newcap = h->cap * 1.5) == 0)
147 t = recallocarray(h->lines, h->cap, newcap,
148 sizeof(h->lines[0]));
150 return got_error_from_errno("recallocarray");
155 if ((t = strdup(line)) == NULL)
156 return got_error_from_errno("strdup");
158 h->lines[h->len++] = t;
162 static const struct got_error *
163 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
165 const struct got_error *err = NULL;
167 struct got_imsg_patch_hunk hdr;
168 struct got_imsg_patch patch;
169 struct got_patch_hunk *h = NULL;
173 memset(p, 0, sizeof(*p));
174 STAILQ_INIT(&p->head);
176 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
179 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
183 if (imsg.hdr.type != GOT_IMSG_PATCH) {
184 err = got_error(GOT_ERR_PRIVSEP_MSG);
187 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
188 if (datalen != sizeof(patch)) {
189 err = got_error(GOT_ERR_PRIVSEP_LEN);
192 memcpy(&patch, imsg.data, sizeof(patch));
194 if (patch.old[sizeof(patch.old)-1] != '\0' ||
195 patch.new[sizeof(patch.new)-1] != '\0' ||
196 patch.cid[sizeof(patch.cid)-1] != '\0' ||
197 patch.blob[sizeof(patch.blob)-1] != '\0') {
198 err = got_error(GOT_ERR_PRIVSEP_LEN);
202 if (*patch.cid != '\0')
203 strlcpy(p->cid, patch.cid, sizeof(p->cid));
205 if (*patch.blob != '\0')
206 strlcpy(p->blob, patch.blob, sizeof(p->blob));
208 p->xbit = patch.xbit;
210 /* automatically set strip=1 for git-style diffs */
211 if (strip == -1 && patch.git &&
212 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
213 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
216 /* prefer the new name if not /dev/null for not git-style diffs */
217 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
218 err = got_path_strip(&p->old, patch.new, strip);
221 } else if (*patch.old != '\0') {
222 err = got_path_strip(&p->old, patch.old, strip);
227 if (*patch.new != '\0') {
228 err = got_path_strip(&p->new, patch.new, strip);
233 if (p->old == NULL && p->new == NULL) {
234 err = got_error(GOT_ERR_PATCH_MALFORMED);
243 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
249 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
250 switch (imsg.hdr.type) {
251 case GOT_IMSG_PATCH_DONE:
252 if (h != NULL && h->len == 0)
253 err = got_error(GOT_ERR_PATCH_MALFORMED);
255 case GOT_IMSG_PATCH_HUNK:
257 (h->len == 0 || h->old_nonl || h->new_nonl)) {
258 err = got_error(GOT_ERR_PATCH_MALFORMED);
262 if (datalen != sizeof(hdr)) {
263 err = got_error(GOT_ERR_PRIVSEP_LEN);
266 memcpy(&hdr, imsg.data, sizeof(hdr));
267 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
268 err = got_error(GOT_ERR_PRIVSEP_LEN);
271 if ((h = calloc(1, sizeof(*h))) == NULL) {
272 err = got_error_from_errno("calloc");
275 h->old_from = hdr.oldfrom;
276 h->old_lines = hdr.oldlines;
277 h->new_from = hdr.newfrom;
278 h->new_lines = hdr.newlines;
279 STAILQ_INSERT_TAIL(&p->head, h, entries);
281 case GOT_IMSG_PATCH_LINE:
283 err = got_error(GOT_ERR_PRIVSEP_MSG);
287 /* at least one char */
288 if (datalen < 2 || t[datalen-1] != '\0') {
289 err = got_error(GOT_ERR_PRIVSEP_MSG);
292 if (*t != ' ' && *t != '-' && *t != '+' &&
294 err = got_error(GOT_ERR_PRIVSEP_MSG);
299 err = pushline(h, t);
300 else if (lastmode == '-')
302 else if (lastmode == '+')
305 err = got_error(GOT_ERR_PATCH_MALFORMED);
313 err = got_error(GOT_ERR_PRIVSEP_MSG);
329 reverse_patch(struct got_patch *p)
331 struct got_patch_hunk *h;
335 STAILQ_FOREACH(h, &p->head, entries) {
337 h->old_from = h->new_from;
341 h->old_lines = h->new_lines;
345 h->old_nonl = h->new_nonl;
348 for (i = 0; i < h->len; ++i) {
349 if (*h->lines[i] == '+')
351 else if (*h->lines[i] == '-')
358 * Copy data from orig starting at copypos until pos into tmp.
359 * If pos is -1, copy until EOF.
361 static const struct got_error *
362 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
367 if (fseeko(orig, copypos, SEEK_SET) == -1)
368 return got_error_from_errno("fseeko");
370 while (pos == -1 || copypos < pos) {
373 len = MIN(len, (size_t)pos - copypos);
374 r = fread(buf, 1, len, orig);
375 if (r != len && ferror(orig))
376 return got_error_from_errno("fread");
377 w = fwrite(buf, 1, r, tmp);
379 return got_error_from_errno("fwrite");
381 if (r != len && feof(orig)) {
384 return got_error(GOT_ERR_HUNK_FAILED);
390 static int linecmp(const char *, const char *, int *);
392 static const struct got_error *
393 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
395 const struct got_error *err = NULL;
397 char mode = *h->lines[0];
401 int mangled = 0, match_lineno = -1;
404 linelen = getline(&line, &linesize, orig);
407 err = got_error_from_errno("getline");
408 else if (match == -1)
409 err = got_error(GOT_ERR_HUNK_FAILED);
412 if (line[linelen - 1] == '\n')
413 line[linelen - 1] = '\0';
416 if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
417 (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
418 (mode == '+' && *lineno == h->old_from)) {
419 match = ftello(orig);
421 err = got_error_from_errno("ftello");
425 match_lineno = (*lineno)-1;
428 if (*lineno >= h->old_from && match != -1) {
437 *lineno = match_lineno;
438 if (fseeko(orig, match, SEEK_SET) == -1)
439 err = got_error_from_errno("fseeko");
447 linecmp(const char *a, const char *b, int *mangled)
458 while (*a == '\t' || *a == ' ' || *a == '\f')
460 while (*b == '\t' || *b == ' ' || *b == '\f')
462 if (*a == '\0' || *a != *b)
470 static const struct got_error *
471 test_hunk(FILE *orig, struct got_patch_hunk *h)
473 const struct got_error *err = NULL;
475 size_t linesize = 0, i = 0;
479 for (i = 0; i < h->len; ++i) {
480 switch (*h->lines[i]) {
485 linelen = getline(&line, &linesize, orig);
488 err = got_error_from_errno("getline");
491 GOT_ERR_HUNK_FAILED);
494 if (line[linelen - 1] == '\n')
495 line[linelen - 1] = '\0';
496 if (linecmp(h->lines[i] + 1, line, &mangled)) {
497 err = got_error(GOT_ERR_HUNK_FAILED);
511 static const struct got_error *
512 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
515 const struct got_error *err = NULL;
517 size_t linesize = 0, i, new = 0;
522 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
523 return got_error_from_errno("fseeko");
525 for (i = 0; i < h->len; ++i) {
526 switch (mode = *h->lines[i]) {
531 linelen = getline(&line, &linesize, orig);
533 err = got_error_from_errno("getline");
536 if (line[linelen - 1] == '\n')
537 line[linelen - 1] = '\0';
543 if (fprintf(tmp, "%s\n", t) < 0) {
544 err = got_error_from_errno("fprintf");
550 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
551 err = got_error_from_errno("fprintf");
554 if (new != h->new_lines || !h->new_nonl) {
555 if (fprintf(tmp, "\n") < 0) {
556 err = got_error_from_errno("fprintf");
569 static const struct got_error *
570 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
572 const struct got_error *err = NULL;
573 struct got_patch_hunk *h;
581 if (p->old == NULL) { /* create */
582 h = STAILQ_FIRST(&p->head);
583 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
584 return got_error(GOT_ERR_PATCH_MALFORMED);
585 return apply_hunk(orig, tmp, h, &lineno, 0);
588 /* When deleting binary files there are no hunks to apply. */
589 if (p->new == NULL && STAILQ_EMPTY(&p->head))
592 if (fstat(fileno(orig), &sb) == -1)
593 return got_error_from_errno("fstat");
596 STAILQ_FOREACH(h, &p->head, entries) {
598 err = locate_hunk(orig, h, &pos, &lineno);
599 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
603 err = copy(tmp, orig, copypos, pos);
608 err = test_hunk(orig, h);
609 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
611 * try to apply the hunk again starting the search
612 * after the previous partial match.
614 if (fseeko(orig, pos, SEEK_SET) == -1)
615 return got_error_from_errno("fseeko");
616 linelen = getline(&line, &linesize, orig);
618 return got_error_from_errno("getline");
625 if (lineno + 1 != h->old_from)
626 h->offset = lineno + 1 - h->old_from;
628 err = apply_hunk(orig, tmp, h, &lineno, pos);
632 copypos = ftello(orig);
634 return got_error_from_errno("ftello");
637 if (p->new == NULL && sb.st_size != copypos) {
638 h = STAILQ_FIRST(&p->head);
639 h->err = got_error(GOT_ERR_HUNK_FAILED);
641 } else if (!feof(orig))
642 err = copy(tmp, orig, copypos, -1);
647 static const struct got_error *
648 report_progress(struct patch_args *pa, const char *old, const char *new,
649 unsigned char status, const struct got_error *orig_error)
651 const struct got_error *err;
652 struct got_patch_hunk *h;
654 err = pa->progress_cb(pa->progress_arg, old, new, status,
655 orig_error, 0, 0, 0, 0, 0, 0, NULL);
659 STAILQ_FOREACH(h, pa->head, entries) {
660 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
663 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
664 h->old_from, h->old_lines, h->new_from, h->new_lines,
665 h->offset, h->ws_mangled, h->err);
673 static const struct got_error *
674 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
677 return report_progress(arg, path, NULL, status, NULL);
680 static const struct got_error *
681 patch_add(void *arg, unsigned char status, const char *path)
683 return report_progress(arg, NULL, path, status, NULL);
686 static const struct got_error *
687 open_blob(char **path, FILE **fp, const char *blobid,
688 struct got_repository *repo)
690 const struct got_error *err = NULL;
691 struct got_blob_object *blob = NULL;
692 struct got_object_id id, *idptr, *matched_id = NULL;
698 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
699 err = got_repo_match_object_id(&matched_id, NULL, blobid,
700 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
706 if (!got_parse_sha1_digest(id.sha1, blobid))
707 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
711 fd = got_opentempfd();
713 err = got_error_from_errno("got_opentempfd");
717 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
721 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
726 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
731 if (fd != -1 && close(fd) == -1 && err == NULL)
732 err = got_error_from_errno("close");
734 got_object_blob_close(blob);
735 if (matched_id != NULL)
749 static const struct got_error *
750 prepare_merge(int *do_merge, char **apath, FILE **afile,
751 struct got_worktree *worktree, struct got_repository *repo,
752 struct got_patch *p, struct got_object_id *commit_id,
753 struct got_tree_object *tree, const char *path)
755 const struct got_error *err = NULL;
761 /* don't run the diff3 merge on creations/deletions */
762 if (p->old == NULL || p->new == NULL)
766 struct got_object_id *id;
768 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
771 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
772 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
774 err = open_blob(apath, afile, p->blob, repo);
775 *do_merge = err == NULL;
776 } else if (*p->blob != '\0') {
777 err = open_blob(apath, afile, p->blob, repo);
779 * ignore failures to open this blob, we might have
782 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
783 err->code != GOT_ERR_NO_OBJ)
785 *do_merge = err == NULL;
792 static const struct got_error *
793 apply_patch(int *overlapcnt, struct got_worktree *worktree,
794 struct got_repository *repo, struct got_fileindex *fileindex,
795 const char *old, const char *new, struct got_patch *p, int nop,
796 int reverse, struct got_object_id *commit_id,
797 struct got_tree_object *tree, struct patch_args *pa,
798 got_cancel_cb cancel_cb, void *cancel_arg)
800 const struct got_error *err = NULL;
802 int do_merge = 0, file_renamed = 0;
803 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
804 char *oldpath = NULL, *newpath = NULL;
805 char *tmppath = NULL, *template = NULL;
806 char *apath = NULL, *mergepath = NULL;
807 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
809 mode_t mode = GOT_DEFAULT_FILE_MODE;
813 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
814 commit_id, tree, old);
818 if (reverse && !do_merge)
821 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
823 err = got_error_from_errno("asprintf");
827 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
829 err = got_error_from_errno("asprintf");
833 file_renamed = strcmp(oldpath, newpath);
835 if (asprintf(&template, "%s/got-patch",
836 got_worktree_get_root_path(worktree)) == -1) {
837 err = got_error_from_errno(template);
841 if (p->old != NULL) {
842 if ((oldfile = fopen(oldpath, "r")) == NULL) {
843 err = got_error_from_errno2("open", oldpath);
846 if (fstat(fileno(oldfile), &sb) == -1) {
847 err = got_error_from_errno2("fstat", oldpath);
852 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
854 err = got_opentemp_named(&tmppath, &tmpfile, template, "");
857 outfd = fileno(tmpfile);
858 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
863 const char *type, *id;
865 if (fseeko(afile, 0, SEEK_SET) == -1 ||
866 fseeko(oldfile, 0, SEEK_SET) == -1 ||
867 fseeko(tmpfile, 0, SEEK_SET) == -1) {
868 err = got_error_from_errno("fseeko");
872 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
873 err = got_error_from_errno("asprintf");
878 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
879 err = got_error_from_errno("asprintf");
884 if (*p->cid != '\0') {
892 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
893 err = got_error_from_errno("asprintf");
911 err = got_opentemp_named(&mergepath, &mergefile, template, "");
914 outfd = fileno(mergefile);
916 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
917 oldfile, oldlabel, anclabel, newlabel,
918 GOT_DIFF_ALGORITHM_PATIENCE);
926 if (p->old != NULL && p->new == NULL) {
927 err = got_worktree_patch_schedule_rm(old, repo, worktree,
928 fileindex, patch_delete, pa);
932 if (fchmod(outfd, apply_umask(mode)) == -1) {
933 err = got_error_from_errno2("chmod", tmppath);
938 err = got_path_move_file(mergepath, newpath);
944 err = got_path_move_file(tmppath, newpath);
952 err = got_worktree_patch_schedule_rm(old, repo, worktree,
953 fileindex, patch_delete, pa);
955 err = got_worktree_patch_schedule_add(new, repo,
956 worktree, fileindex, patch_add,
960 } else if (p->old == NULL) {
961 err = got_worktree_patch_schedule_add(new, repo, worktree,
962 fileindex, patch_add, pa);
965 } else if (*overlapcnt != 0)
966 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
968 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
970 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
975 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
976 err = got_error_from_errno("unlink");
977 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
978 err = got_error_from_errno("fclose");
982 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
983 err = got_error_from_errno("fclose");
985 if (apath != NULL && unlink(apath) == -1 && err == NULL)
986 err = got_error_from_errno("unlink");
987 if (afile != NULL && fclose(afile) == EOF && err == NULL)
988 err = got_error_from_errno("fclose");
991 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
992 err = got_error_from_errno("unlink");
993 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
994 err = got_error_from_errno("fclose");
1004 const struct got_error *
1005 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1006 int nop, int strip, int reverse, struct got_object_id *commit_id,
1007 got_patch_progress_cb progress_cb, void *progress_arg,
1008 got_cancel_cb cancel_cb, void *cancel_arg)
1010 const struct got_error *err = NULL, *complete_err = NULL;
1011 struct got_fileindex *fileindex = NULL;
1012 struct got_commit_object *commit = NULL;
1013 struct got_tree_object *tree = NULL;
1014 char *fileindex_path = NULL;
1015 char *oldpath, *newpath;
1016 struct imsgbuf *ibuf;
1017 int imsg_fds[2] = {-1, -1};
1018 int overlapcnt, done = 0, failed = 0;
1021 ibuf = calloc(1, sizeof(*ibuf));
1023 err = got_error_from_errno("calloc");
1027 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1028 err = got_error_from_errno("socketpair");
1034 err = got_error_from_errno("fork");
1036 } else if (pid == 0) {
1037 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1042 if (close(imsg_fds[1]) == -1) {
1043 err = got_error_from_errno("close");
1047 imsg_init(ibuf, imsg_fds[0]);
1049 err = send_patch(ibuf, fd);
1054 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1060 err = got_object_open_as_commit(&commit, repo, commit_id);
1064 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1069 while (!done && err == NULL) {
1071 struct patch_args pa;
1073 pa.progress_cb = progress_cb;
1074 pa.progress_arg = progress_arg;
1077 err = recv_patch(ibuf, &done, &p, strip);
1081 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1082 &newpath, worktree, repo, fileindex);
1084 err = apply_patch(&overlapcnt, worktree, repo,
1085 fileindex, oldpath, newpath, &p, nop, reverse,
1086 commit_id, tree, &pa, cancel_cb, cancel_arg);
1089 /* recoverable errors */
1090 if (err->code == GOT_ERR_FILE_STATUS ||
1091 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1092 err = report_progress(&pa, p.old, p.new,
1093 GOT_STATUS_CANNOT_UPDATE, err);
1094 else if (err->code == GOT_ERR_HUNK_FAILED)
1095 err = report_progress(&pa, p.old, p.new,
1096 GOT_STATUS_CANNOT_UPDATE, NULL);
1098 if (overlapcnt != 0)
1110 if (fileindex != NULL)
1111 complete_err = got_worktree_patch_complete(fileindex,
1113 if (complete_err && err == NULL)
1115 free(fileindex_path);
1117 got_object_tree_close(tree);
1119 got_object_commit_close(commit);
1120 if (fd != -1 && close(fd) == -1 && err == NULL)
1121 err = got_error_from_errno("close");
1124 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1125 err = got_error_from_errno("close");
1126 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1127 err = got_error_from_errno("close");
1128 if (err == NULL && failed)
1129 err = got_error(GOT_ERR_PATCH_FAILED);