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;
89 static const struct got_error *
90 send_patch(struct imsgbuf *ibuf, int fd)
92 const struct got_error *err = NULL;
94 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
96 err = got_error_from_errno(
97 "imsg_compose GOT_IMSG_PATCH_FILE");
102 return got_privsep_flush_imsg(ibuf);
106 patch_free(struct got_patch *p)
108 struct got_patch_hunk *h;
111 while (!STAILQ_EMPTY(&p->head)) {
112 h = STAILQ_FIRST(&p->head);
113 STAILQ_REMOVE_HEAD(&p->head, entries);
115 for (i = 0; i < h->len; ++i)
124 memset(p, 0, sizeof(*p));
125 STAILQ_INIT(&p->head);
128 static const struct got_error *
129 pushline(struct got_patch_hunk *h, const char *line)
134 if (h->len == h->cap) {
135 if ((newcap = h->cap * 1.5) == 0)
137 t = recallocarray(h->lines, h->cap, newcap,
138 sizeof(h->lines[0]));
140 return got_error_from_errno("recallocarray");
145 if ((t = strdup(line)) == NULL)
146 return got_error_from_errno("strdup");
148 h->lines[h->len++] = t;
152 static const struct got_error *
153 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
155 const struct got_error *err = NULL;
157 struct got_imsg_patch_hunk hdr;
158 struct got_imsg_patch patch;
159 struct got_patch_hunk *h = NULL;
163 memset(p, 0, sizeof(*p));
164 STAILQ_INIT(&p->head);
166 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
169 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
173 if (imsg.hdr.type != GOT_IMSG_PATCH) {
174 err = got_error(GOT_ERR_PRIVSEP_MSG);
177 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
178 if (datalen != sizeof(patch)) {
179 err = got_error(GOT_ERR_PRIVSEP_LEN);
182 memcpy(&patch, imsg.data, sizeof(patch));
184 if (patch.old[sizeof(patch.old)-1] != '\0' ||
185 patch.new[sizeof(patch.new)-1] != '\0' ||
186 patch.cid[sizeof(patch.cid)-1] != '\0' ||
187 patch.blob[sizeof(patch.blob)-1] != '\0') {
188 err = got_error(GOT_ERR_PRIVSEP_LEN);
192 if (*patch.cid != '\0')
193 strlcpy(p->cid, patch.cid, sizeof(p->cid));
195 if (*patch.blob != '\0')
196 strlcpy(p->blob, patch.blob, sizeof(p->blob));
198 p->xbit = patch.xbit;
200 /* automatically set strip=1 for git-style diffs */
201 if (strip == -1 && patch.git &&
202 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
203 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
206 /* prefer the new name if not /dev/null for not git-style diffs */
207 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
208 err = got_path_strip(&p->old, patch.new, strip);
211 } else if (*patch.old != '\0') {
212 err = got_path_strip(&p->old, patch.old, strip);
217 if (*patch.new != '\0') {
218 err = got_path_strip(&p->new, patch.new, strip);
223 if (p->old == NULL && p->new == NULL) {
224 err = got_error(GOT_ERR_PATCH_MALFORMED);
233 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
239 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
240 switch (imsg.hdr.type) {
241 case GOT_IMSG_PATCH_DONE:
242 if (h != NULL && h->len == 0)
243 err = got_error(GOT_ERR_PATCH_MALFORMED);
245 case GOT_IMSG_PATCH_HUNK:
247 (h->len == 0 || h->old_nonl || h->new_nonl)) {
248 err = got_error(GOT_ERR_PATCH_MALFORMED);
252 if (datalen != sizeof(hdr)) {
253 err = got_error(GOT_ERR_PRIVSEP_LEN);
256 memcpy(&hdr, imsg.data, sizeof(hdr));
257 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
258 err = got_error(GOT_ERR_PRIVSEP_LEN);
261 if ((h = calloc(1, sizeof(*h))) == NULL) {
262 err = got_error_from_errno("calloc");
265 h->old_from = hdr.oldfrom;
266 h->old_lines = hdr.oldlines;
267 h->new_from = hdr.newfrom;
268 h->new_lines = hdr.newlines;
269 STAILQ_INSERT_TAIL(&p->head, h, entries);
271 case GOT_IMSG_PATCH_LINE:
273 err = got_error(GOT_ERR_PRIVSEP_MSG);
277 /* at least one char */
278 if (datalen < 2 || t[datalen-1] != '\0') {
279 err = got_error(GOT_ERR_PRIVSEP_MSG);
282 if (*t != ' ' && *t != '-' && *t != '+' &&
284 err = got_error(GOT_ERR_PRIVSEP_MSG);
289 err = pushline(h, t);
290 else if (lastmode == '-')
292 else if (lastmode == '+')
295 err = got_error(GOT_ERR_PATCH_MALFORMED);
303 err = got_error(GOT_ERR_PRIVSEP_MSG);
319 reverse_patch(struct got_patch *p)
321 struct got_patch_hunk *h;
325 STAILQ_FOREACH(h, &p->head, entries) {
327 h->old_from = h->new_from;
331 h->old_lines = h->new_lines;
335 h->old_nonl = h->new_nonl;
338 for (i = 0; i < h->len; ++i) {
339 if (*h->lines[i] == '+')
341 else if (*h->lines[i] == '-')
348 * Copy data from orig starting at copypos until pos into tmp.
349 * If pos is -1, copy until EOF.
351 static const struct got_error *
352 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
357 if (fseeko(orig, copypos, SEEK_SET) == -1)
358 return got_error_from_errno("fseeko");
360 while (pos == -1 || copypos < pos) {
363 len = MIN(len, (size_t)pos - copypos);
364 r = fread(buf, 1, len, orig);
365 if (r != len && ferror(orig))
366 return got_error_from_errno("fread");
367 w = fwrite(buf, 1, r, tmp);
369 return got_error_from_errno("fwrite");
371 if (r != len && feof(orig)) {
374 return got_error(GOT_ERR_HUNK_FAILED);
380 static int linecmp(const char *, const char *, int *);
382 static const struct got_error *
383 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
385 const struct got_error *err = NULL;
387 char mode = *h->lines[0];
391 int mangled = 0, match_lineno = -1;
394 linelen = getline(&line, &linesize, orig);
397 err = got_error_from_errno("getline");
398 else if (match == -1)
399 err = got_error(GOT_ERR_HUNK_FAILED);
402 if (line[linelen - 1] == '\n')
403 line[linelen - 1] = '\0';
406 if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
407 (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
408 (mode == '+' && *lineno == h->old_from)) {
409 match = ftello(orig);
411 err = got_error_from_errno("ftello");
415 match_lineno = (*lineno)-1;
418 if (*lineno >= h->old_from && match != -1) {
427 *lineno = match_lineno;
428 if (fseeko(orig, match, SEEK_SET) == -1)
429 err = got_error_from_errno("fseeko");
437 linecmp(const char *a, const char *b, int *mangled)
448 while (*a == '\t' || *a == ' ' || *a == '\f')
450 while (*b == '\t' || *b == ' ' || *b == '\f')
452 if (*a == '\0' || *a != *b)
460 static const struct got_error *
461 test_hunk(FILE *orig, struct got_patch_hunk *h)
463 const struct got_error *err = NULL;
465 size_t linesize = 0, i = 0;
469 for (i = 0; i < h->len; ++i) {
470 switch (*h->lines[i]) {
475 linelen = getline(&line, &linesize, orig);
478 err = got_error_from_errno("getline");
481 GOT_ERR_HUNK_FAILED);
484 if (line[linelen - 1] == '\n')
485 line[linelen - 1] = '\0';
486 if (linecmp(h->lines[i] + 1, line, &mangled)) {
487 err = got_error(GOT_ERR_HUNK_FAILED);
501 static const struct got_error *
502 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
505 const struct got_error *err = NULL;
507 size_t linesize = 0, i, new = 0;
512 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
513 return got_error_from_errno("fseeko");
515 for (i = 0; i < h->len; ++i) {
516 switch (mode = *h->lines[i]) {
521 linelen = getline(&line, &linesize, orig);
523 err = got_error_from_errno("getline");
526 if (line[linelen - 1] == '\n')
527 line[linelen - 1] = '\0';
533 if (fprintf(tmp, "%s\n", t) < 0) {
534 err = got_error_from_errno("fprintf");
540 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
541 err = got_error_from_errno("fprintf");
544 if (new != h->new_lines || !h->new_nonl) {
545 if (fprintf(tmp, "\n") < 0) {
546 err = got_error_from_errno("fprintf");
559 static const struct got_error *
560 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
562 const struct got_error *err = NULL;
563 struct got_patch_hunk *h;
571 if (p->old == NULL) { /* create */
572 h = STAILQ_FIRST(&p->head);
573 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
574 return got_error(GOT_ERR_PATCH_MALFORMED);
575 return apply_hunk(orig, tmp, h, &lineno, 0);
578 if (fstat(fileno(orig), &sb) == -1)
579 return got_error_from_errno("fstat");
582 STAILQ_FOREACH(h, &p->head, entries) {
584 err = locate_hunk(orig, h, &pos, &lineno);
585 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
589 err = copy(tmp, orig, copypos, pos);
594 err = test_hunk(orig, h);
595 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
597 * try to apply the hunk again starting the search
598 * after the previous partial match.
600 if (fseeko(orig, pos, SEEK_SET) == -1)
601 return got_error_from_errno("fseeko");
602 linelen = getline(&line, &linesize, orig);
604 return got_error_from_errno("getline");
611 if (lineno + 1 != h->old_from)
612 h->offset = lineno + 1 - h->old_from;
614 err = apply_hunk(orig, tmp, h, &lineno, pos);
618 copypos = ftello(orig);
620 return got_error_from_errno("ftello");
623 if (p->new == NULL && sb.st_size != copypos) {
624 h = STAILQ_FIRST(&p->head);
625 h->err = got_error(GOT_ERR_HUNK_FAILED);
627 } else if (!feof(orig))
628 err = copy(tmp, orig, copypos, -1);
633 static const struct got_error *
634 report_progress(struct patch_args *pa, const char *old, const char *new,
635 unsigned char status, const struct got_error *orig_error)
637 const struct got_error *err;
638 struct got_patch_hunk *h;
640 err = pa->progress_cb(pa->progress_arg, old, new, status,
641 orig_error, 0, 0, 0, 0, 0, 0, NULL);
645 STAILQ_FOREACH(h, pa->head, entries) {
646 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
649 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
650 h->old_from, h->old_lines, h->new_from, h->new_lines,
651 h->offset, h->ws_mangled, h->err);
659 static const struct got_error *
660 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
663 return report_progress(arg, path, NULL, status, NULL);
666 static const struct got_error *
667 patch_add(void *arg, unsigned char status, const char *path)
669 return report_progress(arg, NULL, path, status, NULL);
672 static const struct got_error *
673 open_blob(char **path, FILE **fp, const char *blobid,
674 struct got_repository *repo)
676 const struct got_error *err = NULL;
677 struct got_blob_object *blob = NULL;
678 struct got_object_id id, *idptr, *matched_id = NULL;
684 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
685 err = got_repo_match_object_id(&matched_id, NULL, blobid,
686 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
692 if (!got_parse_sha1_digest(id.sha1, blobid))
693 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
697 fd = got_opentempfd();
699 err = got_error_from_errno("got_opentempfd");
703 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
707 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
711 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
716 if (fd != -1 && close(fd) == -1 && err == NULL)
717 err = got_error_from_errno("close");
719 got_object_blob_close(blob);
720 if (matched_id != NULL)
734 static const struct got_error *
735 prepare_merge(int *do_merge, char **apath, FILE **afile,
736 struct got_worktree *worktree, struct got_repository *repo,
737 struct got_patch *p, struct got_object_id *commit_id,
738 struct got_tree_object *tree, const char *path)
740 const struct got_error *err = NULL;
746 /* don't run the diff3 merge on creations/deletions */
747 if (p->old == NULL || p->new == NULL)
751 struct got_object_id *id;
753 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
756 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
757 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
759 err = open_blob(apath, afile, p->blob, repo);
760 *do_merge = err == NULL;
761 } else if (*p->blob != '\0') {
762 err = open_blob(apath, afile, p->blob, repo);
764 * ignore failures to open this blob, we might have
767 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
768 err->code != GOT_ERR_NO_OBJ)
770 *do_merge = err == NULL;
777 static const struct got_error *
778 apply_patch(int *overlapcnt, struct got_worktree *worktree,
779 struct got_repository *repo, struct got_fileindex *fileindex,
780 const char *old, const char *new, struct got_patch *p, int nop,
781 int reverse, struct got_object_id *commit_id,
782 struct got_tree_object *tree, struct patch_args *pa,
783 got_cancel_cb cancel_cb, void *cancel_arg)
785 const struct got_error *err = NULL;
787 int do_merge = 0, file_renamed = 0;
788 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
789 char *oldpath = NULL, *newpath = NULL;
790 char *tmppath = NULL, *template = NULL;
791 char *apath = NULL, *mergepath = NULL;
792 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
794 mode_t mode = GOT_DEFAULT_FILE_MODE;
798 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
799 commit_id, tree, old);
803 if (reverse && !do_merge)
806 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
808 err = got_error_from_errno("asprintf");
812 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
814 err = got_error_from_errno("asprintf");
818 file_renamed = strcmp(oldpath, newpath);
820 if (asprintf(&template, "%s/got-patch",
821 got_worktree_get_root_path(worktree)) == -1) {
822 err = got_error_from_errno(template);
826 if (p->old != NULL) {
827 if ((oldfile = fopen(oldpath, "r")) == NULL) {
828 err = got_error_from_errno2("open", oldpath);
831 if (fstat(fileno(oldfile), &sb) == -1) {
832 err = got_error_from_errno2("fstat", oldpath);
837 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
839 err = got_opentemp_named(&tmppath, &tmpfile, template);
842 outfd = fileno(tmpfile);
843 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
848 const char *type, *id;
850 if (fseeko(afile, 0, SEEK_SET) == -1 ||
851 fseeko(oldfile, 0, SEEK_SET) == -1 ||
852 fseeko(tmpfile, 0, SEEK_SET) == -1) {
853 err = got_error_from_errno("fseeko");
857 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
858 err = got_error_from_errno("asprintf");
863 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
864 err = got_error_from_errno("asprintf");
869 if (*p->cid != '\0') {
877 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
878 err = got_error_from_errno("asprintf");
896 err = got_opentemp_named(&mergepath, &mergefile, template);
899 outfd = fileno(mergefile);
901 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
902 oldfile, oldlabel, anclabel, newlabel,
903 GOT_DIFF_ALGORITHM_PATIENCE);
911 if (p->old != NULL && p->new == NULL) {
912 err = got_worktree_patch_schedule_rm(old, repo, worktree,
913 fileindex, patch_delete, pa);
917 if (fchmod(outfd, mode) == -1) {
918 err = got_error_from_errno2("chmod", tmppath);
923 err = got_path_move_file(mergepath, newpath);
929 err = got_path_move_file(tmppath, newpath);
937 err = got_worktree_patch_schedule_rm(old, repo, worktree,
938 fileindex, patch_delete, pa);
940 err = got_worktree_patch_schedule_add(new, repo,
941 worktree, fileindex, patch_add,
945 } else if (p->old == NULL) {
946 err = got_worktree_patch_schedule_add(new, repo, worktree,
947 fileindex, patch_add, pa);
950 } else if (*overlapcnt != 0)
951 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
953 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
955 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
960 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
961 err = got_error_from_errno("unlink");
962 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
963 err = got_error_from_errno("fclose");
967 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
968 err = got_error_from_errno("fclose");
970 if (apath != NULL && unlink(apath) == -1 && err == NULL)
971 err = got_error_from_errno("unlink");
972 if (afile != NULL && fclose(afile) == EOF && err == NULL)
973 err = got_error_from_errno("fclose");
976 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
977 err = got_error_from_errno("unlink");
978 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
979 err = got_error_from_errno("fclose");
989 const struct got_error *
990 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
991 int nop, int strip, int reverse, struct got_object_id *commit_id,
992 got_patch_progress_cb progress_cb, void *progress_arg,
993 got_cancel_cb cancel_cb, void *cancel_arg)
995 const struct got_error *err = NULL, *complete_err = NULL;
996 struct got_fileindex *fileindex = NULL;
997 struct got_commit_object *commit = NULL;
998 struct got_tree_object *tree = NULL;
999 char *fileindex_path = NULL;
1000 char *oldpath, *newpath;
1001 struct imsgbuf *ibuf;
1002 int imsg_fds[2] = {-1, -1};
1003 int overlapcnt, done = 0, failed = 0;
1006 ibuf = calloc(1, sizeof(*ibuf));
1008 err = got_error_from_errno("calloc");
1012 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1013 err = got_error_from_errno("socketpair");
1019 err = got_error_from_errno("fork");
1021 } else if (pid == 0) {
1022 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1027 if (close(imsg_fds[1]) == -1) {
1028 err = got_error_from_errno("close");
1032 imsg_init(ibuf, imsg_fds[0]);
1034 err = send_patch(ibuf, fd);
1039 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1045 err = got_object_open_as_commit(&commit, repo, commit_id);
1049 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1054 while (!done && err == NULL) {
1056 struct patch_args pa;
1058 pa.progress_cb = progress_cb;
1059 pa.progress_arg = progress_arg;
1062 err = recv_patch(ibuf, &done, &p, strip);
1066 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1067 &newpath, worktree, repo, fileindex);
1069 err = apply_patch(&overlapcnt, worktree, repo,
1070 fileindex, oldpath, newpath, &p, nop, reverse,
1071 commit_id, tree, &pa, cancel_cb, cancel_arg);
1074 /* recoverable errors */
1075 if (err->code == GOT_ERR_FILE_STATUS ||
1076 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1077 err = report_progress(&pa, p.old, p.new,
1078 GOT_STATUS_CANNOT_UPDATE, err);
1079 else if (err->code == GOT_ERR_HUNK_FAILED)
1080 err = report_progress(&pa, p.old, p.new,
1081 GOT_STATUS_CANNOT_UPDATE, NULL);
1083 if (overlapcnt != 0)
1095 if (fileindex != NULL)
1096 complete_err = got_worktree_patch_complete(fileindex,
1098 if (complete_err && err == NULL)
1100 free(fileindex_path);
1102 got_object_tree_close(tree);
1104 got_object_commit_close(commit);
1105 if (fd != -1 && close(fd) == -1 && err == NULL)
1106 err = got_error_from_errno("close");
1109 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1110 err = got_error_from_errno("close");
1111 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1112 err = got_error_from_errno("close");
1113 if (err == NULL && failed)
1114 err = got_error(GOT_ERR_PATCH_FAILED);