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;
91 static const struct got_error *
92 send_patch(struct imsgbuf *ibuf, int fd)
94 const struct got_error *err = NULL;
96 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
98 err = got_error_from_errno(
99 "imsg_compose GOT_IMSG_PATCH_FILE");
104 return got_privsep_flush_imsg(ibuf);
108 patch_free(struct got_patch *p)
110 struct got_patch_hunk *h;
113 while (!STAILQ_EMPTY(&p->head)) {
114 h = STAILQ_FIRST(&p->head);
115 STAILQ_REMOVE_HEAD(&p->head, entries);
117 for (i = 0; i < h->len; ++i)
126 memset(p, 0, sizeof(*p));
127 STAILQ_INIT(&p->head);
130 static const struct got_error *
131 pushline(struct got_patch_hunk *h, const char *line)
136 if (h->len == h->cap) {
137 if ((newcap = h->cap * 1.5) == 0)
139 t = recallocarray(h->lines, h->cap, newcap,
140 sizeof(h->lines[0]));
142 return got_error_from_errno("recallocarray");
147 if ((t = strdup(line)) == NULL)
148 return got_error_from_errno("strdup");
150 h->lines[h->len++] = t;
154 static const struct got_error *
155 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
157 const struct got_error *err = NULL;
159 struct got_imsg_patch_hunk hdr;
160 struct got_imsg_patch patch;
161 struct got_patch_hunk *h = NULL;
165 memset(p, 0, sizeof(*p));
166 STAILQ_INIT(&p->head);
168 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
171 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
175 if (imsg.hdr.type != GOT_IMSG_PATCH) {
176 err = got_error(GOT_ERR_PRIVSEP_MSG);
179 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
180 if (datalen != sizeof(patch)) {
181 err = got_error(GOT_ERR_PRIVSEP_LEN);
184 memcpy(&patch, imsg.data, sizeof(patch));
186 if (patch.old[sizeof(patch.old)-1] != '\0' ||
187 patch.new[sizeof(patch.new)-1] != '\0' ||
188 patch.cid[sizeof(patch.cid)-1] != '\0' ||
189 patch.blob[sizeof(patch.blob)-1] != '\0') {
190 err = got_error(GOT_ERR_PRIVSEP_LEN);
194 if (*patch.cid != '\0')
195 strlcpy(p->cid, patch.cid, sizeof(p->cid));
197 if (*patch.blob != '\0')
198 strlcpy(p->blob, patch.blob, sizeof(p->blob));
200 p->xbit = patch.xbit;
202 /* automatically set strip=1 for git-style diffs */
203 if (strip == -1 && patch.git &&
204 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
205 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
208 /* prefer the new name if not /dev/null for not git-style diffs */
209 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
210 err = got_path_strip(&p->old, patch.new, strip);
213 } else if (*patch.old != '\0') {
214 err = got_path_strip(&p->old, patch.old, strip);
219 if (*patch.new != '\0') {
220 err = got_path_strip(&p->new, patch.new, strip);
225 if (p->old == NULL && p->new == NULL) {
226 err = got_error(GOT_ERR_PATCH_MALFORMED);
235 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
241 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
242 switch (imsg.hdr.type) {
243 case GOT_IMSG_PATCH_DONE:
244 if (h != NULL && h->len == 0)
245 err = got_error(GOT_ERR_PATCH_MALFORMED);
247 case GOT_IMSG_PATCH_HUNK:
249 (h->len == 0 || h->old_nonl || h->new_nonl)) {
250 err = got_error(GOT_ERR_PATCH_MALFORMED);
254 if (datalen != sizeof(hdr)) {
255 err = got_error(GOT_ERR_PRIVSEP_LEN);
258 memcpy(&hdr, imsg.data, sizeof(hdr));
259 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
260 err = got_error(GOT_ERR_PRIVSEP_LEN);
263 if ((h = calloc(1, sizeof(*h))) == NULL) {
264 err = got_error_from_errno("calloc");
267 h->old_from = hdr.oldfrom;
268 h->old_lines = hdr.oldlines;
269 h->new_from = hdr.newfrom;
270 h->new_lines = hdr.newlines;
271 STAILQ_INSERT_TAIL(&p->head, h, entries);
273 case GOT_IMSG_PATCH_LINE:
275 err = got_error(GOT_ERR_PRIVSEP_MSG);
279 /* at least one char */
280 if (datalen < 2 || t[datalen-1] != '\0') {
281 err = got_error(GOT_ERR_PRIVSEP_MSG);
284 if (*t != ' ' && *t != '-' && *t != '+' &&
286 err = got_error(GOT_ERR_PRIVSEP_MSG);
291 err = pushline(h, t);
292 else if (lastmode == '-')
294 else if (lastmode == '+')
297 err = got_error(GOT_ERR_PATCH_MALFORMED);
305 err = got_error(GOT_ERR_PRIVSEP_MSG);
321 reverse_patch(struct got_patch *p)
323 struct got_patch_hunk *h;
327 STAILQ_FOREACH(h, &p->head, entries) {
329 h->old_from = h->new_from;
333 h->old_lines = h->new_lines;
337 h->old_nonl = h->new_nonl;
340 for (i = 0; i < h->len; ++i) {
341 if (*h->lines[i] == '+')
343 else if (*h->lines[i] == '-')
350 * Copy data from orig starting at copypos until pos into tmp.
351 * If pos is -1, copy until EOF.
353 static const struct got_error *
354 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
359 if (fseeko(orig, copypos, SEEK_SET) == -1)
360 return got_error_from_errno("fseeko");
362 while (pos == -1 || copypos < pos) {
365 len = MIN(len, (size_t)pos - copypos);
366 r = fread(buf, 1, len, orig);
367 if (r != len && ferror(orig))
368 return got_error_from_errno("fread");
369 w = fwrite(buf, 1, r, tmp);
371 return got_error_from_errno("fwrite");
373 if (r != len && feof(orig)) {
376 return got_error(GOT_ERR_HUNK_FAILED);
382 static int linecmp(const char *, const char *, int *);
384 static const struct got_error *
385 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
387 const struct got_error *err = NULL;
389 char mode = *h->lines[0];
393 int mangled = 0, match_lineno = -1;
396 linelen = getline(&line, &linesize, orig);
399 err = got_error_from_errno("getline");
400 else if (match == -1)
401 err = got_error(GOT_ERR_HUNK_FAILED);
404 if (line[linelen - 1] == '\n')
405 line[linelen - 1] = '\0';
408 if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
409 (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
410 (mode == '+' && *lineno == h->old_from)) {
411 match = ftello(orig);
413 err = got_error_from_errno("ftello");
417 match_lineno = (*lineno)-1;
420 if (*lineno >= h->old_from && match != -1) {
429 *lineno = match_lineno;
430 if (fseeko(orig, match, SEEK_SET) == -1)
431 err = got_error_from_errno("fseeko");
439 linecmp(const char *a, const char *b, int *mangled)
450 while (*a == '\t' || *a == ' ' || *a == '\f')
452 while (*b == '\t' || *b == ' ' || *b == '\f')
454 if (*a == '\0' || *a != *b)
462 static const struct got_error *
463 test_hunk(FILE *orig, struct got_patch_hunk *h)
465 const struct got_error *err = NULL;
467 size_t linesize = 0, i = 0;
471 for (i = 0; i < h->len; ++i) {
472 switch (*h->lines[i]) {
477 linelen = getline(&line, &linesize, orig);
480 err = got_error_from_errno("getline");
483 GOT_ERR_HUNK_FAILED);
486 if (line[linelen - 1] == '\n')
487 line[linelen - 1] = '\0';
488 if (linecmp(h->lines[i] + 1, line, &mangled)) {
489 err = got_error(GOT_ERR_HUNK_FAILED);
503 static const struct got_error *
504 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
507 const struct got_error *err = NULL;
509 size_t linesize = 0, i, new = 0;
514 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
515 return got_error_from_errno("fseeko");
517 for (i = 0; i < h->len; ++i) {
518 switch (mode = *h->lines[i]) {
523 linelen = getline(&line, &linesize, orig);
525 err = got_error_from_errno("getline");
528 if (line[linelen - 1] == '\n')
529 line[linelen - 1] = '\0';
535 if (fprintf(tmp, "%s\n", t) < 0) {
536 err = got_error_from_errno("fprintf");
542 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
543 err = got_error_from_errno("fprintf");
546 if (new != h->new_lines || !h->new_nonl) {
547 if (fprintf(tmp, "\n") < 0) {
548 err = got_error_from_errno("fprintf");
561 static const struct got_error *
562 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
564 const struct got_error *err = NULL;
565 struct got_patch_hunk *h;
573 if (p->old == NULL) { /* create */
574 h = STAILQ_FIRST(&p->head);
575 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
576 return got_error(GOT_ERR_PATCH_MALFORMED);
577 return apply_hunk(orig, tmp, h, &lineno, 0);
580 if (fstat(fileno(orig), &sb) == -1)
581 return got_error_from_errno("fstat");
584 STAILQ_FOREACH(h, &p->head, entries) {
586 err = locate_hunk(orig, h, &pos, &lineno);
587 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
591 err = copy(tmp, orig, copypos, pos);
596 err = test_hunk(orig, h);
597 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
599 * try to apply the hunk again starting the search
600 * after the previous partial match.
602 if (fseeko(orig, pos, SEEK_SET) == -1)
603 return got_error_from_errno("fseeko");
604 linelen = getline(&line, &linesize, orig);
606 return got_error_from_errno("getline");
613 if (lineno + 1 != h->old_from)
614 h->offset = lineno + 1 - h->old_from;
616 err = apply_hunk(orig, tmp, h, &lineno, pos);
620 copypos = ftello(orig);
622 return got_error_from_errno("ftello");
625 if (p->new == NULL && sb.st_size != copypos) {
626 h = STAILQ_FIRST(&p->head);
627 h->err = got_error(GOT_ERR_HUNK_FAILED);
629 } else if (!feof(orig))
630 err = copy(tmp, orig, copypos, -1);
635 static const struct got_error *
636 report_progress(struct patch_args *pa, const char *old, const char *new,
637 unsigned char status, const struct got_error *orig_error)
639 const struct got_error *err;
640 struct got_patch_hunk *h;
642 err = pa->progress_cb(pa->progress_arg, old, new, status,
643 orig_error, 0, 0, 0, 0, 0, 0, NULL);
647 STAILQ_FOREACH(h, pa->head, entries) {
648 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
651 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
652 h->old_from, h->old_lines, h->new_from, h->new_lines,
653 h->offset, h->ws_mangled, h->err);
661 static const struct got_error *
662 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
665 return report_progress(arg, path, NULL, status, NULL);
668 static const struct got_error *
669 patch_add(void *arg, unsigned char status, const char *path)
671 return report_progress(arg, NULL, path, status, NULL);
674 static const struct got_error *
675 open_blob(char **path, FILE **fp, const char *blobid,
676 struct got_repository *repo)
678 const struct got_error *err = NULL;
679 struct got_blob_object *blob = NULL;
680 struct got_object_id id, *idptr, *matched_id = NULL;
686 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
687 err = got_repo_match_object_id(&matched_id, NULL, blobid,
688 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
694 if (!got_parse_sha1_digest(id.sha1, blobid))
695 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
699 fd = got_opentempfd();
701 err = got_error_from_errno("got_opentempfd");
705 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
709 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
713 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
718 if (fd != -1 && close(fd) == -1 && err == NULL)
719 err = got_error_from_errno("close");
721 got_object_blob_close(blob);
722 if (matched_id != NULL)
736 static const struct got_error *
737 prepare_merge(int *do_merge, char **apath, FILE **afile,
738 struct got_worktree *worktree, struct got_repository *repo,
739 struct got_patch *p, struct got_object_id *commit_id,
740 struct got_tree_object *tree, const char *path)
742 const struct got_error *err = NULL;
748 /* don't run the diff3 merge on creations/deletions */
749 if (p->old == NULL || p->new == NULL)
753 struct got_object_id *id;
755 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
758 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
759 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
761 err = open_blob(apath, afile, p->blob, repo);
762 *do_merge = err == NULL;
763 } else if (*p->blob != '\0') {
764 err = open_blob(apath, afile, p->blob, repo);
766 * ignore failures to open this blob, we might have
769 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
770 err->code != GOT_ERR_NO_OBJ)
772 *do_merge = err == NULL;
779 static const struct got_error *
780 apply_patch(int *overlapcnt, struct got_worktree *worktree,
781 struct got_repository *repo, struct got_fileindex *fileindex,
782 const char *old, const char *new, struct got_patch *p, int nop,
783 int reverse, struct got_object_id *commit_id,
784 struct got_tree_object *tree, struct patch_args *pa,
785 got_cancel_cb cancel_cb, void *cancel_arg)
787 const struct got_error *err = NULL;
789 int do_merge = 0, file_renamed = 0;
790 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
791 char *oldpath = NULL, *newpath = NULL;
792 char *tmppath = NULL, *template = NULL, *parent = NULL;
793 char *apath = NULL, *mergepath = NULL;
794 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
797 mode_t mode = GOT_DEFAULT_FILE_MODE;
801 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
802 commit_id, tree, old);
806 if (reverse && !do_merge)
809 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
811 err = got_error_from_errno("asprintf");
815 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
817 err = got_error_from_errno("asprintf");
821 file_renamed = strcmp(oldpath, newpath);
823 if (asprintf(&template, "%s/got-patch",
824 got_worktree_get_root_path(worktree)) == -1) {
825 err = got_error_from_errno(template);
829 if (p->old != NULL) {
830 if ((oldfile = fopen(oldpath, "r")) == NULL) {
831 err = got_error_from_errno2("open", oldpath);
834 if (fstat(fileno(oldfile), &sb) == -1) {
835 err = got_error_from_errno2("fstat", oldpath);
840 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
842 err = got_opentemp_named(&tmppath, &tmpfile, template);
846 outfd = fileno(tmpfile);
847 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
852 const char *type, *id;
854 if (fseeko(afile, 0, SEEK_SET) == -1 ||
855 fseeko(oldfile, 0, SEEK_SET) == -1 ||
856 fseeko(tmpfile, 0, SEEK_SET) == -1) {
857 err = got_error_from_errno("fseeko");
861 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
862 err = got_error_from_errno("asprintf");
867 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
868 err = got_error_from_errno("asprintf");
873 if (*p->cid != '\0') {
881 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
882 err = got_error_from_errno("asprintf");
900 err = got_opentemp_named(&mergepath, &mergefile, template);
904 outfd = fileno(mergefile);
906 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
907 oldfile, oldlabel, anclabel, newlabel,
908 GOT_DIFF_ALGORITHM_PATIENCE);
916 if (p->old != NULL && p->new == NULL) {
917 err = got_worktree_patch_schedule_rm(old, repo, worktree,
918 fileindex, patch_delete, pa);
922 if (fchmod(outfd, mode) == -1) {
923 err = got_error_from_errno2("chmod", tmppath);
927 if (rename(outpath, newpath) == -1) {
928 if (errno != ENOENT) {
929 err = got_error_from_errno3("rename", outpath,
934 err = got_path_dirname(&parent, newpath);
937 err = got_path_mkdir(parent);
940 if (rename(outpath, newpath) == -1) {
941 err = got_error_from_errno3("rename", outpath,
948 err = got_worktree_patch_schedule_rm(old, repo, worktree,
949 fileindex, patch_delete, pa);
951 err = got_worktree_patch_schedule_add(new, repo,
952 worktree, fileindex, patch_add,
956 } else if (p->old == NULL) {
957 err = got_worktree_patch_schedule_add(new, repo, worktree,
958 fileindex, patch_add, pa);
961 } else if (*overlapcnt != 0)
962 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
964 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
966 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
974 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
975 err = got_error_from_errno("fclose");
979 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
980 err = got_error_from_errno("fclose");
984 if (afile != NULL && fclose(afile) == EOF && err == NULL)
985 err = got_error_from_errno("fclose");
988 if (mergepath != NULL)
990 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
991 err = got_error_from_errno("fclose");
1001 const struct got_error *
1002 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1003 int nop, int strip, int reverse, struct got_object_id *commit_id,
1004 got_patch_progress_cb progress_cb, void *progress_arg,
1005 got_cancel_cb cancel_cb, void *cancel_arg)
1007 const struct got_error *err = NULL, *complete_err = NULL;
1008 struct got_fileindex *fileindex = NULL;
1009 struct got_commit_object *commit = NULL;
1010 struct got_tree_object *tree = NULL;
1011 char *fileindex_path = NULL;
1012 char *oldpath, *newpath;
1013 struct imsgbuf *ibuf;
1014 int imsg_fds[2] = {-1, -1};
1015 int overlapcnt, done = 0, failed = 0;
1018 ibuf = calloc(1, sizeof(*ibuf));
1020 err = got_error_from_errno("calloc");
1024 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1025 err = got_error_from_errno("socketpair");
1031 err = got_error_from_errno("fork");
1033 } else if (pid == 0) {
1034 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1039 if (close(imsg_fds[1]) == -1) {
1040 err = got_error_from_errno("close");
1044 imsg_init(ibuf, imsg_fds[0]);
1046 err = send_patch(ibuf, fd);
1051 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1057 err = got_object_open_as_commit(&commit, repo, commit_id);
1061 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1066 while (!done && err == NULL) {
1068 struct patch_args pa;
1070 pa.progress_cb = progress_cb;
1071 pa.progress_arg = progress_arg;
1074 err = recv_patch(ibuf, &done, &p, strip);
1078 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1079 &newpath, worktree, repo, fileindex);
1081 err = apply_patch(&overlapcnt, worktree, repo,
1082 fileindex, oldpath, newpath, &p, nop, reverse,
1083 commit_id, tree, &pa, cancel_cb, cancel_arg);
1086 /* recoverable errors */
1087 if (err->code == GOT_ERR_FILE_STATUS ||
1088 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1089 err = report_progress(&pa, p.old, p.new,
1090 GOT_STATUS_CANNOT_UPDATE, err);
1091 else if (err->code == GOT_ERR_HUNK_FAILED)
1092 err = report_progress(&pa, p.old, p.new,
1093 GOT_STATUS_CANNOT_UPDATE, NULL);
1095 if (overlapcnt != 0)
1107 if (fileindex != NULL)
1108 complete_err = got_worktree_patch_complete(fileindex,
1110 if (complete_err && err == NULL)
1112 free(fileindex_path);
1114 got_object_tree_close(tree);
1116 got_object_commit_close(commit);
1117 if (fd != -1 && close(fd) == -1 && err == NULL)
1118 err = got_error_from_errno("close");
1121 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1122 err = got_error_from_errno("close");
1123 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1124 err = got_error_from_errno("close");
1125 if (err == NULL && failed)
1126 err = got_error(GOT_ERR_PATCH_FAILED);