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 if (fstat(fileno(orig), &sb) == -1)
591 return got_error_from_errno("fstat");
594 STAILQ_FOREACH(h, &p->head, entries) {
596 err = locate_hunk(orig, h, &pos, &lineno);
597 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
601 err = copy(tmp, orig, copypos, pos);
606 err = test_hunk(orig, h);
607 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
609 * try to apply the hunk again starting the search
610 * after the previous partial match.
612 if (fseeko(orig, pos, SEEK_SET) == -1)
613 return got_error_from_errno("fseeko");
614 linelen = getline(&line, &linesize, orig);
616 return got_error_from_errno("getline");
623 if (lineno + 1 != h->old_from)
624 h->offset = lineno + 1 - h->old_from;
626 err = apply_hunk(orig, tmp, h, &lineno, pos);
630 copypos = ftello(orig);
632 return got_error_from_errno("ftello");
635 if (p->new == NULL && sb.st_size != copypos) {
636 h = STAILQ_FIRST(&p->head);
637 h->err = got_error(GOT_ERR_HUNK_FAILED);
639 } else if (!feof(orig))
640 err = copy(tmp, orig, copypos, -1);
645 static const struct got_error *
646 report_progress(struct patch_args *pa, const char *old, const char *new,
647 unsigned char status, const struct got_error *orig_error)
649 const struct got_error *err;
650 struct got_patch_hunk *h;
652 err = pa->progress_cb(pa->progress_arg, old, new, status,
653 orig_error, 0, 0, 0, 0, 0, 0, NULL);
657 STAILQ_FOREACH(h, pa->head, entries) {
658 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
661 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
662 h->old_from, h->old_lines, h->new_from, h->new_lines,
663 h->offset, h->ws_mangled, h->err);
671 static const struct got_error *
672 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
675 return report_progress(arg, path, NULL, status, NULL);
678 static const struct got_error *
679 patch_add(void *arg, unsigned char status, const char *path)
681 return report_progress(arg, NULL, path, status, NULL);
684 static const struct got_error *
685 open_blob(char **path, FILE **fp, const char *blobid,
686 struct got_repository *repo)
688 const struct got_error *err = NULL;
689 struct got_blob_object *blob = NULL;
690 struct got_object_id id, *idptr, *matched_id = NULL;
696 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
697 err = got_repo_match_object_id(&matched_id, NULL, blobid,
698 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
704 if (!got_parse_sha1_digest(id.sha1, blobid))
705 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
709 fd = got_opentempfd();
711 err = got_error_from_errno("got_opentempfd");
715 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
719 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
724 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
729 if (fd != -1 && close(fd) == -1 && err == NULL)
730 err = got_error_from_errno("close");
732 got_object_blob_close(blob);
733 if (matched_id != NULL)
747 static const struct got_error *
748 prepare_merge(int *do_merge, char **apath, FILE **afile,
749 struct got_worktree *worktree, struct got_repository *repo,
750 struct got_patch *p, struct got_object_id *commit_id,
751 struct got_tree_object *tree, const char *path)
753 const struct got_error *err = NULL;
759 /* don't run the diff3 merge on creations/deletions */
760 if (p->old == NULL || p->new == NULL)
764 struct got_object_id *id;
766 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
769 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
770 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
772 err = open_blob(apath, afile, p->blob, repo);
773 *do_merge = err == NULL;
774 } else if (*p->blob != '\0') {
775 err = open_blob(apath, afile, p->blob, repo);
777 * ignore failures to open this blob, we might have
780 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
781 err->code != GOT_ERR_NO_OBJ)
783 *do_merge = err == NULL;
790 static const struct got_error *
791 apply_patch(int *overlapcnt, struct got_worktree *worktree,
792 struct got_repository *repo, struct got_fileindex *fileindex,
793 const char *old, const char *new, struct got_patch *p, int nop,
794 int reverse, struct got_object_id *commit_id,
795 struct got_tree_object *tree, struct patch_args *pa,
796 got_cancel_cb cancel_cb, void *cancel_arg)
798 const struct got_error *err = NULL;
800 int do_merge = 0, file_renamed = 0;
801 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
802 char *oldpath = NULL, *newpath = NULL;
803 char *tmppath = NULL, *template = NULL;
804 char *apath = NULL, *mergepath = NULL;
805 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
807 mode_t mode = GOT_DEFAULT_FILE_MODE;
811 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
812 commit_id, tree, old);
816 if (reverse && !do_merge)
819 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
821 err = got_error_from_errno("asprintf");
825 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
827 err = got_error_from_errno("asprintf");
831 file_renamed = strcmp(oldpath, newpath);
833 if (asprintf(&template, "%s/got-patch",
834 got_worktree_get_root_path(worktree)) == -1) {
835 err = got_error_from_errno(template);
839 if (p->old != NULL) {
840 if ((oldfile = fopen(oldpath, "r")) == NULL) {
841 err = got_error_from_errno2("open", oldpath);
844 if (fstat(fileno(oldfile), &sb) == -1) {
845 err = got_error_from_errno2("fstat", oldpath);
850 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
852 err = got_opentemp_named(&tmppath, &tmpfile, template, "");
855 outfd = fileno(tmpfile);
856 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
861 const char *type, *id;
863 if (fseeko(afile, 0, SEEK_SET) == -1 ||
864 fseeko(oldfile, 0, SEEK_SET) == -1 ||
865 fseeko(tmpfile, 0, SEEK_SET) == -1) {
866 err = got_error_from_errno("fseeko");
870 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
871 err = got_error_from_errno("asprintf");
876 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
877 err = got_error_from_errno("asprintf");
882 if (*p->cid != '\0') {
890 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
891 err = got_error_from_errno("asprintf");
909 err = got_opentemp_named(&mergepath, &mergefile, template, "");
912 outfd = fileno(mergefile);
914 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
915 oldfile, oldlabel, anclabel, newlabel,
916 GOT_DIFF_ALGORITHM_PATIENCE);
924 if (p->old != NULL && p->new == NULL) {
925 err = got_worktree_patch_schedule_rm(old, repo, worktree,
926 fileindex, patch_delete, pa);
930 if (fchmod(outfd, apply_umask(mode)) == -1) {
931 err = got_error_from_errno2("chmod", tmppath);
936 err = got_path_move_file(mergepath, newpath);
942 err = got_path_move_file(tmppath, newpath);
950 err = got_worktree_patch_schedule_rm(old, repo, worktree,
951 fileindex, patch_delete, pa);
953 err = got_worktree_patch_schedule_add(new, repo,
954 worktree, fileindex, patch_add,
958 } else if (p->old == NULL) {
959 err = got_worktree_patch_schedule_add(new, repo, worktree,
960 fileindex, patch_add, pa);
963 } else if (*overlapcnt != 0)
964 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
966 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
968 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
973 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
974 err = got_error_from_errno("unlink");
975 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
976 err = got_error_from_errno("fclose");
980 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
981 err = got_error_from_errno("fclose");
983 if (apath != NULL && unlink(apath) == -1 && err == NULL)
984 err = got_error_from_errno("unlink");
985 if (afile != NULL && fclose(afile) == EOF && err == NULL)
986 err = got_error_from_errno("fclose");
989 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
990 err = got_error_from_errno("unlink");
991 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
992 err = got_error_from_errno("fclose");
1002 const struct got_error *
1003 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1004 int nop, int strip, int reverse, struct got_object_id *commit_id,
1005 got_patch_progress_cb progress_cb, void *progress_arg,
1006 got_cancel_cb cancel_cb, void *cancel_arg)
1008 const struct got_error *err = NULL, *complete_err = NULL;
1009 struct got_fileindex *fileindex = NULL;
1010 struct got_commit_object *commit = NULL;
1011 struct got_tree_object *tree = NULL;
1012 char *fileindex_path = NULL;
1013 char *oldpath, *newpath;
1014 struct imsgbuf *ibuf;
1015 int imsg_fds[2] = {-1, -1};
1016 int overlapcnt, done = 0, failed = 0;
1019 ibuf = calloc(1, sizeof(*ibuf));
1021 err = got_error_from_errno("calloc");
1025 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1026 err = got_error_from_errno("socketpair");
1032 err = got_error_from_errno("fork");
1034 } else if (pid == 0) {
1035 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1040 if (close(imsg_fds[1]) == -1) {
1041 err = got_error_from_errno("close");
1045 imsg_init(ibuf, imsg_fds[0]);
1047 err = send_patch(ibuf, fd);
1052 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1058 err = got_object_open_as_commit(&commit, repo, commit_id);
1062 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1067 while (!done && err == NULL) {
1069 struct patch_args pa;
1071 pa.progress_cb = progress_cb;
1072 pa.progress_arg = progress_arg;
1075 err = recv_patch(ibuf, &done, &p, strip);
1079 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1080 &newpath, worktree, repo, fileindex);
1082 err = apply_patch(&overlapcnt, worktree, repo,
1083 fileindex, oldpath, newpath, &p, nop, reverse,
1084 commit_id, tree, &pa, cancel_cb, cancel_arg);
1087 /* recoverable errors */
1088 if (err->code == GOT_ERR_FILE_STATUS ||
1089 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1090 err = report_progress(&pa, p.old, p.new,
1091 GOT_STATUS_CANNOT_UPDATE, err);
1092 else if (err->code == GOT_ERR_HUNK_FAILED)
1093 err = report_progress(&pa, p.old, p.new,
1094 GOT_STATUS_CANNOT_UPDATE, NULL);
1096 if (overlapcnt != 0)
1108 if (fileindex != NULL)
1109 complete_err = got_worktree_patch_complete(fileindex,
1111 if (complete_err && err == NULL)
1113 free(fileindex_path);
1115 got_object_tree_close(tree);
1117 got_object_commit_close(commit);
1118 if (fd != -1 && close(fd) == -1 && err == NULL)
1119 err = got_error_from_errno("close");
1122 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1123 err = got_error_from_errno("close");
1124 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1125 err = got_error_from_errno("close");
1126 if (err == NULL && failed)
1127 err = got_error(GOT_ERR_PATCH_FAILED);