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>
39 #include "got_error.h"
40 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_repository.h"
46 #include "got_opentemp.h"
47 #include "got_patch.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_diff.h"
52 #include "got_lib_object.h"
53 #include "got_lib_privsep.h"
54 #include "got_lib_hash.h"
56 #define MIN(a, b) ((a) < (b) ? (a) : (b))
58 struct got_patch_hunk {
59 STAILQ_ENTRY(got_patch_hunk) entries;
60 const struct got_error *err;
74 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
81 struct got_patch_hunk_head head;
85 got_patch_progress_cb progress_cb;
87 struct got_patch_hunk_head *head;
91 apply_umask(mode_t mode)
100 static const struct got_error *
101 send_patch(struct imsgbuf *ibuf, int fd)
103 const struct got_error *err = NULL;
105 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
107 err = got_error_from_errno(
108 "imsg_compose GOT_IMSG_PATCH_FILE");
113 return got_privsep_flush_imsg(ibuf);
117 patch_free(struct got_patch *p)
119 struct got_patch_hunk *h;
122 while (!STAILQ_EMPTY(&p->head)) {
123 h = STAILQ_FIRST(&p->head);
124 STAILQ_REMOVE_HEAD(&p->head, entries);
126 for (i = 0; i < h->len; ++i)
135 memset(p, 0, sizeof(*p));
136 STAILQ_INIT(&p->head);
139 static const struct got_error *
140 pushline(struct got_patch_hunk *h, const char *line)
145 if (h->len == h->cap) {
146 if ((newcap = h->cap * 1.5) == 0)
148 t = recallocarray(h->lines, h->cap, newcap,
149 sizeof(h->lines[0]));
151 return got_error_from_errno("recallocarray");
156 if ((t = strdup(line)) == NULL)
157 return got_error_from_errno("strdup");
159 h->lines[h->len++] = t;
163 static const struct got_error *
164 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
166 const struct got_error *err = NULL;
168 struct got_imsg_patch_hunk hdr;
169 struct got_imsg_patch patch;
170 struct got_patch_hunk *h = NULL;
174 memset(p, 0, sizeof(*p));
175 STAILQ_INIT(&p->head);
177 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
180 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
184 if (imsg.hdr.type != GOT_IMSG_PATCH) {
185 err = got_error(GOT_ERR_PRIVSEP_MSG);
188 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
189 if (datalen != sizeof(patch)) {
190 err = got_error(GOT_ERR_PRIVSEP_LEN);
193 memcpy(&patch, imsg.data, sizeof(patch));
195 if (patch.old[sizeof(patch.old)-1] != '\0' ||
196 patch.new[sizeof(patch.new)-1] != '\0' ||
197 patch.cid[sizeof(patch.cid)-1] != '\0' ||
198 patch.blob[sizeof(patch.blob)-1] != '\0') {
199 err = got_error(GOT_ERR_PRIVSEP_LEN);
203 if (*patch.cid != '\0')
204 strlcpy(p->cid, patch.cid, sizeof(p->cid));
206 if (*patch.blob != '\0')
207 strlcpy(p->blob, patch.blob, sizeof(p->blob));
209 p->xbit = patch.xbit;
211 /* automatically set strip=1 for git-style diffs */
212 if (strip == -1 && patch.git &&
213 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
214 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
217 /* prefer the new name if not /dev/null for not git-style diffs */
218 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
219 err = got_path_strip(&p->old, patch.new, strip);
222 } else if (*patch.old != '\0') {
223 err = got_path_strip(&p->old, patch.old, strip);
228 if (*patch.new != '\0') {
229 err = got_path_strip(&p->new, patch.new, strip);
234 if (p->old == NULL && p->new == NULL) {
235 err = got_error(GOT_ERR_PATCH_MALFORMED);
244 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
250 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
251 switch (imsg.hdr.type) {
252 case GOT_IMSG_PATCH_DONE:
253 if (h != NULL && h->len == 0)
254 err = got_error(GOT_ERR_PATCH_MALFORMED);
256 case GOT_IMSG_PATCH_HUNK:
258 (h->len == 0 || h->old_nonl || h->new_nonl)) {
259 err = got_error(GOT_ERR_PATCH_MALFORMED);
263 if (datalen != sizeof(hdr)) {
264 err = got_error(GOT_ERR_PRIVSEP_LEN);
267 memcpy(&hdr, imsg.data, sizeof(hdr));
268 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
269 err = got_error(GOT_ERR_PRIVSEP_LEN);
272 if ((h = calloc(1, sizeof(*h))) == NULL) {
273 err = got_error_from_errno("calloc");
276 h->old_from = hdr.oldfrom;
277 h->old_lines = hdr.oldlines;
278 h->new_from = hdr.newfrom;
279 h->new_lines = hdr.newlines;
280 STAILQ_INSERT_TAIL(&p->head, h, entries);
282 case GOT_IMSG_PATCH_LINE:
284 err = got_error(GOT_ERR_PRIVSEP_MSG);
288 /* at least one char */
289 if (datalen < 2 || t[datalen-1] != '\0') {
290 err = got_error(GOT_ERR_PRIVSEP_MSG);
293 if (*t != ' ' && *t != '-' && *t != '+' &&
295 err = got_error(GOT_ERR_PRIVSEP_MSG);
300 err = pushline(h, t);
301 else if (lastmode == '-')
303 else if (lastmode == '+')
306 err = got_error(GOT_ERR_PATCH_MALFORMED);
314 err = got_error(GOT_ERR_PRIVSEP_MSG);
330 reverse_patch(struct got_patch *p)
332 struct got_patch_hunk *h;
336 STAILQ_FOREACH(h, &p->head, entries) {
338 h->old_from = h->new_from;
342 h->old_lines = h->new_lines;
346 h->old_nonl = h->new_nonl;
349 for (i = 0; i < h->len; ++i) {
350 if (*h->lines[i] == '+')
352 else if (*h->lines[i] == '-')
359 * Copy data from orig starting at copypos until pos into tmp.
360 * If pos is -1, copy until EOF.
362 static const struct got_error *
363 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
368 if (fseeko(orig, copypos, SEEK_SET) == -1)
369 return got_error_from_errno("fseeko");
371 while (pos == -1 || copypos < pos) {
374 len = MIN(len, (size_t)pos - copypos);
375 r = fread(buf, 1, len, orig);
376 if (r != len && ferror(orig))
377 return got_error_from_errno("fread");
378 w = fwrite(buf, 1, r, tmp);
380 return got_error_from_errno("fwrite");
382 if (r != len && feof(orig)) {
385 return got_error(GOT_ERR_HUNK_FAILED);
391 static int linecmp(const char *, const char *, int *);
393 static const struct got_error *
394 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
396 const struct got_error *err = NULL;
398 char mode = *h->lines[0];
402 int mangled = 0, match_lineno = -1;
405 linelen = getline(&line, &linesize, orig);
408 err = got_error_from_errno("getline");
409 else if (match == -1)
410 err = got_error(GOT_ERR_HUNK_FAILED);
413 if (line[linelen - 1] == '\n')
414 line[linelen - 1] = '\0';
417 if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
418 (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
419 (mode == '+' && *lineno == h->old_from)) {
420 match = ftello(orig);
422 err = got_error_from_errno("ftello");
426 match_lineno = (*lineno)-1;
429 if (*lineno >= h->old_from && match != -1) {
438 *lineno = match_lineno;
439 if (fseeko(orig, match, SEEK_SET) == -1)
440 err = got_error_from_errno("fseeko");
448 linecmp(const char *a, const char *b, int *mangled)
459 while (*a == '\t' || *a == ' ' || *a == '\f')
461 while (*b == '\t' || *b == ' ' || *b == '\f')
463 if (*a == '\0' || *a != *b)
471 static const struct got_error *
472 test_hunk(FILE *orig, struct got_patch_hunk *h)
474 const struct got_error *err = NULL;
476 size_t linesize = 0, i = 0;
480 for (i = 0; i < h->len; ++i) {
481 switch (*h->lines[i]) {
486 linelen = getline(&line, &linesize, orig);
489 err = got_error_from_errno("getline");
492 GOT_ERR_HUNK_FAILED);
495 if (line[linelen - 1] == '\n')
496 line[linelen - 1] = '\0';
497 if (linecmp(h->lines[i] + 1, line, &mangled)) {
498 err = got_error(GOT_ERR_HUNK_FAILED);
512 static const struct got_error *
513 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
516 const struct got_error *err = NULL;
518 size_t linesize = 0, i, new = 0;
523 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
524 return got_error_from_errno("fseeko");
526 for (i = 0; i < h->len; ++i) {
527 switch (mode = *h->lines[i]) {
532 linelen = getline(&line, &linesize, orig);
534 err = got_error_from_errno("getline");
537 if (line[linelen - 1] == '\n')
538 line[linelen - 1] = '\0';
544 if (fprintf(tmp, "%s\n", t) < 0) {
545 err = got_error_from_errno("fprintf");
551 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
552 err = got_error_from_errno("fprintf");
555 if (new != h->new_lines || !h->new_nonl) {
556 if (fprintf(tmp, "\n") < 0) {
557 err = got_error_from_errno("fprintf");
570 static const struct got_error *
571 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
573 const struct got_error *err = NULL;
574 struct got_patch_hunk *h;
582 if (p->old == NULL) { /* create */
583 h = STAILQ_FIRST(&p->head);
584 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
585 return got_error(GOT_ERR_PATCH_MALFORMED);
586 return apply_hunk(orig, tmp, h, &lineno, 0);
589 /* When deleting binary files there are no hunks to apply. */
590 if (p->new == NULL && STAILQ_EMPTY(&p->head))
593 if (fstat(fileno(orig), &sb) == -1)
594 return got_error_from_errno("fstat");
597 STAILQ_FOREACH(h, &p->head, entries) {
599 err = locate_hunk(orig, h, &pos, &lineno);
600 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
604 err = copy(tmp, orig, copypos, pos);
609 err = test_hunk(orig, h);
610 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
612 * try to apply the hunk again starting the search
613 * after the previous partial match.
615 if (fseeko(orig, pos, SEEK_SET) == -1)
616 return got_error_from_errno("fseeko");
617 linelen = getline(&line, &linesize, orig);
619 return got_error_from_errno("getline");
626 if (lineno + 1 != h->old_from)
627 h->offset = lineno + 1 - h->old_from;
629 err = apply_hunk(orig, tmp, h, &lineno, pos);
633 copypos = ftello(orig);
635 return got_error_from_errno("ftello");
638 if (p->new == NULL && sb.st_size != copypos) {
639 h = STAILQ_FIRST(&p->head);
640 h->err = got_error(GOT_ERR_HUNK_FAILED);
642 } else if (!feof(orig))
643 err = copy(tmp, orig, copypos, -1);
648 static const struct got_error *
649 report_progress(struct patch_args *pa, const char *old, const char *new,
650 unsigned char status, const struct got_error *orig_error)
652 const struct got_error *err;
653 struct got_patch_hunk *h;
655 err = pa->progress_cb(pa->progress_arg, old, new, status,
656 orig_error, 0, 0, 0, 0, 0, 0, NULL);
660 STAILQ_FOREACH(h, pa->head, entries) {
661 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
664 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
665 h->old_from, h->old_lines, h->new_from, h->new_lines,
666 h->offset, h->ws_mangled, h->err);
674 static const struct got_error *
675 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
678 return report_progress(arg, path, NULL, status, NULL);
681 static const struct got_error *
682 patch_add(void *arg, unsigned char status, const char *path)
684 return report_progress(arg, NULL, path, status, NULL);
687 static const struct got_error *
688 open_blob(char **path, FILE **fp, const char *blobid,
689 struct got_repository *repo)
691 const struct got_error *err = NULL;
692 struct got_blob_object *blob = NULL;
693 struct got_object_id id, *idptr, *matched_id = NULL;
699 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
700 err = got_repo_match_object_id(&matched_id, NULL, blobid,
701 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
707 if (!got_parse_object_id(&id, blobid, GOT_HASH_SHA1))
708 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
712 fd = got_opentempfd();
714 err = got_error_from_errno("got_opentempfd");
718 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
722 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
727 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
732 if (fd != -1 && close(fd) == -1 && err == NULL)
733 err = got_error_from_errno("close");
735 got_object_blob_close(blob);
736 if (matched_id != NULL)
750 static const struct got_error *
751 prepare_merge(int *do_merge, char **apath, FILE **afile,
752 struct got_worktree *worktree, struct got_repository *repo,
753 struct got_patch *p, struct got_object_id *commit_id,
754 struct got_tree_object *tree, const char *path)
756 const struct got_error *err = NULL;
762 /* don't run the diff3 merge on creations/deletions */
763 if (p->old == NULL || p->new == NULL)
767 struct got_object_id *id;
769 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
772 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
773 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
775 err = open_blob(apath, afile, p->blob, repo);
776 *do_merge = err == NULL;
777 } else if (*p->blob != '\0') {
778 err = open_blob(apath, afile, p->blob, repo);
780 * ignore failures to open this blob, we might have
783 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
784 err->code != GOT_ERR_NO_OBJ)
786 *do_merge = err == NULL;
793 static const struct got_error *
794 apply_patch(int *overlapcnt, struct got_worktree *worktree,
795 struct got_repository *repo, struct got_fileindex *fileindex,
796 const char *old, const char *new, struct got_patch *p, int nop,
797 int reverse, struct got_object_id *commit_id,
798 struct got_tree_object *tree, struct patch_args *pa,
799 got_cancel_cb cancel_cb, void *cancel_arg)
801 const struct got_error *err = NULL;
803 int do_merge = 0, file_renamed = 0;
804 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
805 char *oldpath = NULL, *newpath = NULL;
806 char *tmppath = NULL, *template = NULL;
807 char *apath = NULL, *mergepath = NULL;
808 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
810 mode_t mode = GOT_DEFAULT_FILE_MODE;
814 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
815 commit_id, tree, old);
819 if (reverse && !do_merge)
822 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
824 err = got_error_from_errno("asprintf");
828 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
830 err = got_error_from_errno("asprintf");
834 file_renamed = strcmp(oldpath, newpath);
836 if (asprintf(&template, "%s/got-patch",
837 got_worktree_get_root_path(worktree)) == -1) {
838 err = got_error_from_errno(template);
842 if (p->old != NULL) {
843 if ((oldfile = fopen(oldpath, "r")) == NULL) {
844 err = got_error_from_errno2("open", oldpath);
847 if (fstat(fileno(oldfile), &sb) == -1) {
848 err = got_error_from_errno2("fstat", oldpath);
853 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
855 err = got_opentemp_named(&tmppath, &tmpfile, template, "");
858 outfd = fileno(tmpfile);
859 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
864 const char *type, *id;
866 if (fseeko(afile, 0, SEEK_SET) == -1 ||
867 fseeko(oldfile, 0, SEEK_SET) == -1 ||
868 fseeko(tmpfile, 0, SEEK_SET) == -1) {
869 err = got_error_from_errno("fseeko");
873 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
874 err = got_error_from_errno("asprintf");
879 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
880 err = got_error_from_errno("asprintf");
885 if (*p->cid != '\0') {
893 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
894 err = got_error_from_errno("asprintf");
912 err = got_opentemp_named(&mergepath, &mergefile, template, "");
915 outfd = fileno(mergefile);
917 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
918 oldfile, oldlabel, anclabel, newlabel,
919 GOT_DIFF_ALGORITHM_PATIENCE);
927 if (p->old != NULL && p->new == NULL) {
928 err = got_worktree_patch_schedule_rm(old, repo, worktree,
929 fileindex, patch_delete, pa);
933 if (fchmod(outfd, apply_umask(mode)) == -1) {
934 err = got_error_from_errno2("chmod", tmppath);
939 err = got_path_move_file(mergepath, newpath);
945 err = got_path_move_file(tmppath, newpath);
953 err = got_worktree_patch_schedule_rm(old, repo, worktree,
954 fileindex, patch_delete, pa);
956 err = got_worktree_patch_schedule_add(new, repo,
957 worktree, fileindex, patch_add,
961 } else if (p->old == NULL) {
962 err = got_worktree_patch_schedule_add(new, repo, worktree,
963 fileindex, patch_add, pa);
966 } else if (*overlapcnt != 0)
967 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
969 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
971 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
976 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
977 err = got_error_from_errno("unlink");
978 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
979 err = got_error_from_errno("fclose");
983 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
984 err = got_error_from_errno("fclose");
986 if (apath != NULL && unlink(apath) == -1 && err == NULL)
987 err = got_error_from_errno("unlink");
988 if (afile != NULL && fclose(afile) == EOF && err == NULL)
989 err = got_error_from_errno("fclose");
992 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
993 err = got_error_from_errno("unlink");
994 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
995 err = got_error_from_errno("fclose");
1005 const struct got_error *
1006 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1007 int nop, int strip, int reverse, struct got_object_id *commit_id,
1008 got_patch_progress_cb progress_cb, void *progress_arg,
1009 got_cancel_cb cancel_cb, void *cancel_arg)
1011 const struct got_error *err = NULL, *complete_err = NULL;
1012 struct got_fileindex *fileindex = NULL;
1013 struct got_commit_object *commit = NULL;
1014 struct got_tree_object *tree = NULL;
1015 char *fileindex_path = NULL;
1016 char *oldpath, *newpath;
1017 struct imsgbuf *ibuf;
1018 int imsg_fds[2] = {-1, -1};
1019 int overlapcnt, done = 0, failed = 0;
1022 ibuf = calloc(1, sizeof(*ibuf));
1024 err = got_error_from_errno("calloc");
1028 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1029 err = got_error_from_errno("socketpair");
1035 err = got_error_from_errno("fork");
1037 } else if (pid == 0) {
1038 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1043 if (close(imsg_fds[1]) == -1) {
1044 err = got_error_from_errno("close");
1048 imsg_init(ibuf, imsg_fds[0]);
1050 err = send_patch(ibuf, fd);
1055 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1061 err = got_object_open_as_commit(&commit, repo, commit_id);
1065 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1070 while (!done && err == NULL) {
1072 struct patch_args pa;
1074 pa.progress_cb = progress_cb;
1075 pa.progress_arg = progress_arg;
1078 err = recv_patch(ibuf, &done, &p, strip);
1082 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1083 &newpath, worktree, repo, fileindex);
1085 err = apply_patch(&overlapcnt, worktree, repo,
1086 fileindex, oldpath, newpath, &p, nop, reverse,
1087 commit_id, tree, &pa, cancel_cb, cancel_arg);
1090 /* recoverable errors */
1091 if (err->code == GOT_ERR_FILE_STATUS ||
1092 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1093 err = report_progress(&pa, p.old, p.new,
1094 GOT_STATUS_CANNOT_UPDATE, err);
1095 else if (err->code == GOT_ERR_HUNK_FAILED)
1096 err = report_progress(&pa, p.old, p.new,
1097 GOT_STATUS_CANNOT_UPDATE, NULL);
1099 if (overlapcnt != 0)
1111 if (fileindex != NULL)
1112 complete_err = got_worktree_patch_complete(fileindex,
1114 if (complete_err && err == NULL)
1116 free(fileindex_path);
1118 got_object_tree_close(tree);
1120 got_object_commit_close(commit);
1121 if (fd != -1 && close(fd) == -1 && err == NULL)
1122 err = got_error_from_errno("close");
1125 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1126 err = got_error_from_errno("close");
1127 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1128 err = got_error_from_errno("close");
1129 if (err == NULL && failed)
1130 err = got_error(GOT_ERR_PATCH_FAILED);