Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
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.
7 *
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.
15 *
16 * Apply patches.
17 *
18 * Things that we may want to support:
19 * + support indented patches?
20 * + support other kinds of patches?
21 */
23 #include <sys/types.h>
24 #include <sys/queue.h>
25 #include <sys/socket.h>
26 #include <sys/stat.h>
27 #include <sys/uio.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <sha1.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <imsg.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_path.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"
48 #include "got_diff.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_sha1.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;
61 int offset;
62 int old_nonl;
63 int new_nonl;
64 int old_from;
65 int old_lines;
66 int new_from;
67 int new_lines;
68 size_t len;
69 size_t cap;
70 char **lines;
71 };
73 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
74 struct got_patch {
75 char *old;
76 char *new;
77 char cid[41];
78 char blob[41];
79 struct got_patch_hunk_head head;
80 };
82 struct patch_args {
83 got_patch_progress_cb progress_cb;
84 void *progress_arg;
85 struct got_patch_hunk_head *head;
86 };
88 static const struct got_error *
89 send_patch(struct imsgbuf *ibuf, int fd)
90 {
91 const struct got_error *err = NULL;
93 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
94 NULL, 0) == -1) {
95 err = got_error_from_errno(
96 "imsg_compose GOT_IMSG_PATCH_FILE");
97 close(fd);
98 return err;
99 }
101 if (imsg_flush(ibuf) == -1) {
102 err = got_error_from_errno("imsg_flush");
103 imsg_clear(ibuf);
106 return err;
109 static void
110 patch_free(struct got_patch *p)
112 struct got_patch_hunk *h;
113 size_t i;
115 while (!STAILQ_EMPTY(&p->head)) {
116 h = STAILQ_FIRST(&p->head);
117 STAILQ_REMOVE_HEAD(&p->head, entries);
119 for (i = 0; i < h->len; ++i)
120 free(h->lines[i]);
121 free(h->lines);
122 free(h);
125 free(p->new);
126 free(p->old);
128 memset(p, 0, sizeof(*p));
129 STAILQ_INIT(&p->head);
132 static const struct got_error *
133 pushline(struct got_patch_hunk *h, const char *line)
135 void *t;
136 size_t newcap;
138 if (h->len == h->cap) {
139 if ((newcap = h->cap * 1.5) == 0)
140 newcap = 16;
141 t = recallocarray(h->lines, h->cap, newcap,
142 sizeof(h->lines[0]));
143 if (t == NULL)
144 return got_error_from_errno("recallocarray");
145 h->lines = t;
146 h->cap = newcap;
149 if ((t = strdup(line)) == NULL)
150 return got_error_from_errno("strdup");
152 h->lines[h->len++] = t;
153 return NULL;
156 static const struct got_error *
157 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
159 const struct got_error *err = NULL;
160 struct imsg imsg;
161 struct got_imsg_patch_hunk hdr;
162 struct got_imsg_patch patch;
163 struct got_patch_hunk *h = NULL;
164 size_t datalen;
165 int lastmode = -1;
167 memset(p, 0, sizeof(*p));
168 STAILQ_INIT(&p->head);
170 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
171 if (err)
172 return err;
173 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
174 *done = 1;
175 goto done;
177 if (imsg.hdr.type != GOT_IMSG_PATCH) {
178 err = got_error(GOT_ERR_PRIVSEP_MSG);
179 goto done;
181 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
182 if (datalen != sizeof(patch)) {
183 err = got_error(GOT_ERR_PRIVSEP_LEN);
184 goto done;
186 memcpy(&patch, imsg.data, sizeof(patch));
188 if (patch.old[sizeof(patch.old)-1] != '\0' ||
189 patch.new[sizeof(patch.new)-1] != '\0' ||
190 patch.cid[sizeof(patch.cid)-1] != '\0' ||
191 patch.blob[sizeof(patch.blob)-1] != '\0') {
192 err = got_error(GOT_ERR_PRIVSEP_LEN);
193 goto done;
196 if (*patch.cid != '\0')
197 strlcpy(p->cid, patch.cid, sizeof(p->cid));
199 if (*patch.blob != '\0')
200 strlcpy(p->blob, patch.blob, sizeof(p->blob));
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)))
206 strip = 1;
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);
211 if (err)
212 goto done;
213 } else if (*patch.old != '\0') {
214 err = got_path_strip(&p->old, patch.old, strip);
215 if (err)
216 goto done;
219 if (*patch.new != '\0') {
220 err = got_path_strip(&p->new, patch.new, strip);
221 if (err)
222 goto done;
225 if (p->old == NULL && p->new == NULL) {
226 err = got_error(GOT_ERR_PATCH_MALFORMED);
227 goto done;
230 imsg_free(&imsg);
232 for (;;) {
233 char *t;
235 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
236 if (err) {
237 patch_free(p);
238 return err;
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);
246 goto done;
247 case GOT_IMSG_PATCH_HUNK:
248 if (h != NULL &&
249 (h->len == 0 || h->old_nonl || h->new_nonl)) {
250 err = got_error(GOT_ERR_PATCH_MALFORMED);
251 goto done;
253 lastmode = -1;
254 if (datalen != sizeof(hdr)) {
255 err = got_error(GOT_ERR_PRIVSEP_LEN);
256 goto done;
258 memcpy(&hdr, imsg.data, sizeof(hdr));
259 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
260 err = got_error(GOT_ERR_PRIVSEP_LEN);
261 goto done;
263 if ((h = calloc(1, sizeof(*h))) == NULL) {
264 err = got_error_from_errno("calloc");
265 goto done;
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);
272 break;
273 case GOT_IMSG_PATCH_LINE:
274 if (h == NULL) {
275 err = got_error(GOT_ERR_PRIVSEP_MSG);
276 goto done;
278 t = imsg.data;
279 /* at least one char */
280 if (datalen < 2 || t[datalen-1] != '\0') {
281 err = got_error(GOT_ERR_PRIVSEP_MSG);
282 goto done;
284 if (*t != ' ' && *t != '-' && *t != '+' &&
285 *t != '\\') {
286 err = got_error(GOT_ERR_PRIVSEP_MSG);
287 goto done;
290 if (*t != '\\')
291 err = pushline(h, t);
292 else if (lastmode == '-')
293 h->old_nonl = 1;
294 else if (lastmode == '+')
295 h->new_nonl = 1;
296 else
297 err = got_error(GOT_ERR_PATCH_MALFORMED);
299 if (err)
300 goto done;
302 lastmode = *t;
303 break;
304 default:
305 err = got_error(GOT_ERR_PRIVSEP_MSG);
306 goto done;
309 imsg_free(&imsg);
312 done:
313 if (err)
314 patch_free(p);
316 imsg_free(&imsg);
317 return err;
320 /*
321 * Copy data from orig starting at copypos until pos into tmp.
322 * If pos is -1, copy until EOF.
323 */
324 static const struct got_error *
325 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
327 char buf[BUFSIZ];
328 size_t len, r, w;
330 if (fseeko(orig, copypos, SEEK_SET) == -1)
331 return got_error_from_errno("fseeko");
333 while (pos == -1 || copypos < pos) {
334 len = sizeof(buf);
335 if (pos > 0)
336 len = MIN(len, (size_t)pos - copypos);
337 r = fread(buf, 1, len, orig);
338 if (r != len && ferror(orig))
339 return got_error_from_errno("fread");
340 w = fwrite(buf, 1, r, tmp);
341 if (w != r)
342 return got_error_from_errno("fwrite");
343 copypos += len;
344 if (r != len && feof(orig)) {
345 if (pos == -1)
346 return NULL;
347 return got_error(GOT_ERR_HUNK_FAILED);
350 return NULL;
353 static const struct got_error *
354 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
356 const struct got_error *err = NULL;
357 char *line = NULL;
358 char mode = *h->lines[0];
359 size_t linesize = 0;
360 ssize_t linelen;
361 off_t match = -1;
362 int match_lineno = -1;
364 for (;;) {
365 linelen = getline(&line, &linesize, orig);
366 if (linelen == -1) {
367 if (ferror(orig))
368 err = got_error_from_errno("getline");
369 else if (match == -1)
370 err = got_error(GOT_ERR_HUNK_FAILED);
371 break;
373 if (line[linelen - 1] == '\n')
374 line[linelen - 1] = '\0';
375 (*lineno)++;
377 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
378 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
379 (mode == '+' && *lineno == h->old_from)) {
380 match = ftello(orig);
381 if (match == -1) {
382 err = got_error_from_errno("ftello");
383 break;
385 match -= linelen;
386 match_lineno = (*lineno)-1;
389 if (*lineno >= h->old_from && match != -1)
390 break;
393 if (err == NULL) {
394 *pos = match;
395 *lineno = match_lineno;
396 if (fseeko(orig, match, SEEK_SET) == -1)
397 err = got_error_from_errno("fseeko");
400 free(line);
401 return err;
404 static const struct got_error *
405 test_hunk(FILE *orig, struct got_patch_hunk *h)
407 const struct got_error *err = NULL;
408 char *line = NULL;
409 size_t linesize = 0, i = 0;
410 ssize_t linelen;
412 for (i = 0; i < h->len; ++i) {
413 switch (*h->lines[i]) {
414 case '+':
415 continue;
416 case ' ':
417 case '-':
418 linelen = getline(&line, &linesize, orig);
419 if (linelen == -1) {
420 if (ferror(orig))
421 err = got_error_from_errno("getline");
422 else
423 err = got_error(
424 GOT_ERR_HUNK_FAILED);
425 goto done;
427 if (line[linelen - 1] == '\n')
428 line[linelen - 1] = '\0';
429 if (strcmp(h->lines[i] + 1, line)) {
430 err = got_error(GOT_ERR_HUNK_FAILED);
431 goto done;
433 break;
437 done:
438 free(line);
439 return err;
442 static const struct got_error *
443 apply_hunk(FILE *tmp, struct got_patch_hunk *h, int *lineno)
445 size_t i, new = 0;
447 for (i = 0; i < h->len; ++i) {
448 switch (*h->lines[i]) {
449 case ' ':
450 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
451 return got_error_from_errno("fprintf");
452 /* fallthrough */
453 case '-':
454 (*lineno)++;
455 break;
456 case '+':
457 new++;
458 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
459 return got_error_from_errno("fprintf");
460 if (new != h->new_lines || !h->new_nonl) {
461 if (fprintf(tmp, "\n") < 0)
462 return got_error_from_errno(
463 "fprintf");
465 break;
468 return NULL;
471 static const struct got_error *
472 patch_file(struct got_patch *p, FILE *orig, FILE *tmp, mode_t *mode)
474 const struct got_error *err = NULL;
475 struct got_patch_hunk *h;
476 struct stat sb;
477 int lineno = 0;
478 off_t copypos, pos;
479 char *line = NULL;
480 size_t linesize = 0;
481 ssize_t linelen;
483 if (p->old == NULL) { /* create */
484 h = STAILQ_FIRST(&p->head);
485 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
486 return got_error(GOT_ERR_PATCH_MALFORMED);
487 return apply_hunk(tmp, h, &lineno);
490 if (fstat(fileno(orig), &sb) == -1)
491 return got_error_from_errno("fstat");
492 *mode = sb.st_mode;
494 copypos = 0;
495 STAILQ_FOREACH(h, &p->head, entries) {
496 tryagain:
497 err = locate_hunk(orig, h, &pos, &lineno);
498 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
499 h->err = err;
500 if (err != NULL)
501 return err;
502 err = copy(tmp, orig, copypos, pos);
503 if (err != NULL)
504 return err;
505 copypos = pos;
507 err = test_hunk(orig, h);
508 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
509 /*
510 * try to apply the hunk again starting the search
511 * after the previous partial match.
512 */
513 if (fseeko(orig, pos, SEEK_SET) == -1)
514 return got_error_from_errno("fseeko");
515 linelen = getline(&line, &linesize, orig);
516 if (linelen == -1)
517 return got_error_from_errno("getline");
518 lineno++;
519 goto tryagain;
521 if (err != NULL)
522 return err;
524 if (lineno + 1 != h->old_from)
525 h->offset = lineno + 1 - h->old_from;
527 err = apply_hunk(tmp, h, &lineno);
528 if (err != NULL)
529 return err;
531 copypos = ftello(orig);
532 if (copypos == -1)
533 return got_error_from_errno("ftello");
536 if (p->new == NULL && sb.st_size != copypos) {
537 h = STAILQ_FIRST(&p->head);
538 h->err = got_error(GOT_ERR_HUNK_FAILED);
539 err = h->err;
540 } else if (!feof(orig))
541 err = copy(tmp, orig, copypos, -1);
543 return err;
546 static const struct got_error *
547 report_progress(struct patch_args *pa, const char *old, const char *new,
548 unsigned char status, const struct got_error *orig_error)
550 const struct got_error *err;
551 struct got_patch_hunk *h;
553 err = pa->progress_cb(pa->progress_arg, old, new, status,
554 orig_error, 0, 0, 0, 0, 0, NULL);
555 if (err)
556 return err;
558 STAILQ_FOREACH(h, pa->head, entries) {
559 if (h->offset == 0 && h->err == NULL)
560 continue;
562 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
563 h->old_from, h->old_lines, h->new_from, h->new_lines,
564 h->offset, h->err);
565 if (err)
566 return err;
569 return NULL;
572 static const struct got_error *
573 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
574 const char *path)
576 return report_progress(arg, path, NULL, status, NULL);
579 static const struct got_error *
580 patch_add(void *arg, unsigned char status, const char *path)
582 return report_progress(arg, NULL, path, status, NULL);
585 static const struct got_error *
586 open_blob(char **path, FILE **fp, const char *blobid,
587 struct got_repository *repo)
589 const struct got_error *err = NULL;
590 struct got_blob_object *blob = NULL;
591 struct got_object_id id, *idptr, *matched_id = NULL;
592 int fd = -1;
594 *fp = NULL;
595 *path = NULL;
597 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
598 err = got_repo_match_object_id(&matched_id, NULL, blobid,
599 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
600 repo);
601 if (err)
602 return err;
603 idptr = matched_id;
604 } else {
605 if (!got_parse_sha1_digest(id.sha1, blobid))
606 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
607 idptr = &id;
610 fd = got_opentempfd();
611 if (fd == -1) {
612 err = got_error_from_errno("got_opentempfd");
613 goto done;
616 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
617 if (err)
618 goto done;
620 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
621 if (err)
622 goto done;
624 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
625 if (err)
626 goto done;
628 done:
629 if (fd != -1 && close(fd) == -1 && err == NULL)
630 err = got_error_from_errno("close");
631 if (blob)
632 got_object_blob_close(blob);
633 if (matched_id != NULL)
634 free(matched_id);
635 if (err) {
636 if (*fp != NULL)
637 fclose(*fp);
638 if (*path != NULL)
639 unlink(*path);
640 free(*path);
641 *fp = NULL;
642 *path = NULL;
644 return err;
647 static const struct got_error *
648 apply_patch(int *overlapcnt, struct got_worktree *worktree,
649 struct got_repository *repo, struct got_fileindex *fileindex,
650 const char *old, const char *new, struct got_patch *p, int nop,
651 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
653 const struct got_error *err = NULL;
654 int do_merge = 0, file_renamed = 0;
655 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
656 char *oldpath = NULL, *newpath = NULL;
657 char *tmppath = NULL, *template = NULL, *parent = NULL;
658 char *apath = NULL, *mergepath = NULL;
659 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
660 int outfd;
661 const char *outpath;
662 mode_t mode = GOT_DEFAULT_FILE_MODE;
664 *overlapcnt = 0;
666 /* don't run the diff3 merge on creations/deletions */
667 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
668 err = open_blob(&apath, &afile, p->blob, repo);
669 /*
670 * ignore failures to open this blob, we might have
671 * parsed gibberish.
672 */
673 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
674 err->code != GOT_ERR_NO_OBJ)
675 return err;
676 else if (err == NULL)
677 do_merge = 1;
678 err = NULL;
681 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
682 old) == -1) {
683 err = got_error_from_errno("asprintf");
684 goto done;
687 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
688 new) == -1) {
689 err = got_error_from_errno("asprintf");
690 goto done;
693 file_renamed = strcmp(oldpath, newpath);
695 if (asprintf(&template, "%s/got-patch",
696 got_worktree_get_root_path(worktree)) == -1) {
697 err = got_error_from_errno(template);
698 goto done;
701 if (p->old != NULL && (oldfile = fopen(oldpath, "r")) == NULL) {
702 err = got_error_from_errno2("open", oldpath);
703 goto done;
706 err = got_opentemp_named(&tmppath, &tmpfile, template);
707 if (err)
708 goto done;
709 outpath = tmppath;
710 outfd = fileno(tmpfile);
711 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile, &mode);
712 if (err)
713 goto done;
715 if (do_merge) {
716 const char *type, *id;
718 if (fseeko(afile, 0, SEEK_SET) == -1 ||
719 fseeko(oldfile, 0, SEEK_SET) == -1 ||
720 fseeko(tmpfile, 0, SEEK_SET) == -1) {
721 err = got_error_from_errno("fseeko");
722 goto done;
725 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
726 err = got_error_from_errno("asprintf");
727 oldlabel = NULL;
728 goto done;
731 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
732 err = got_error_from_errno("asprintf");
733 newlabel = NULL;
734 goto done;
737 if (*p->cid != '\0') {
738 type = "commit";
739 id = p->cid;
740 } else {
741 type = "blob";
742 id = p->blob;
745 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
746 err = got_error_from_errno("asprintf");
747 anclabel = NULL;
748 goto done;
751 err = got_opentemp_named(&mergepath, &mergefile, template);
752 if (err)
753 goto done;
754 outpath = mergepath;
755 outfd = fileno(mergefile);
757 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
758 oldfile, oldlabel, anclabel, newlabel,
759 GOT_DIFF_ALGORITHM_PATIENCE);
760 if (err)
761 goto done;
764 if (nop)
765 goto done;
767 if (p->old != NULL && p->new == NULL) {
768 err = got_worktree_patch_schedule_rm(old, repo, worktree,
769 fileindex, patch_delete, pa);
770 goto done;
773 if (fchmod(outfd, mode) == -1) {
774 err = got_error_from_errno2("chmod", tmppath);
775 goto done;
778 if (rename(outpath, newpath) == -1) {
779 if (errno != ENOENT) {
780 err = got_error_from_errno3("rename", outpath,
781 newpath);
782 goto done;
785 err = got_path_dirname(&parent, newpath);
786 if (err != NULL)
787 goto done;
788 err = got_path_mkdir(parent);
789 if (err != NULL)
790 goto done;
791 if (rename(outpath, newpath) == -1) {
792 err = got_error_from_errno3("rename", outpath,
793 newpath);
794 goto done;
798 if (file_renamed) {
799 err = got_worktree_patch_schedule_rm(old, repo, worktree,
800 fileindex, patch_delete, pa);
801 if (err == NULL)
802 err = got_worktree_patch_schedule_add(new, repo,
803 worktree, fileindex, patch_add,
804 pa);
805 if (err)
806 unlink(newpath);
807 } else if (p->old == NULL) {
808 err = got_worktree_patch_schedule_add(new, repo, worktree,
809 fileindex, patch_add, pa);
810 if (err)
811 unlink(newpath);
812 } else if (*overlapcnt != 0)
813 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
814 else if (do_merge)
815 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
816 else
817 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
819 done:
820 free(parent);
821 free(template);
823 if (tmppath != NULL)
824 unlink(tmppath);
825 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
826 err = got_error_from_errno("fclose");
827 free(tmppath);
829 free(oldpath);
830 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
831 err = got_error_from_errno("fclose");
833 if (apath != NULL)
834 unlink(apath);
835 if (afile != NULL && fclose(afile) == EOF && err == NULL)
836 err = got_error_from_errno("fclose");
837 free(apath);
839 if (mergepath != NULL)
840 unlink(mergepath);
841 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
842 err = got_error_from_errno("fclose");
843 free(mergepath);
845 free(newpath);
846 free(oldlabel);
847 free(newlabel);
848 free(anclabel);
849 return err;
852 static void
853 reverse_patch(struct got_patch *p)
855 struct got_patch_hunk *h;
856 size_t i;
857 int tmp;
859 STAILQ_FOREACH(h, &p->head, entries) {
860 tmp = h->old_from;
861 h->old_from = h->new_from;
862 h->new_from = tmp;
864 tmp = h->old_lines;
865 h->old_lines = h->new_lines;
866 h->new_lines = tmp;
868 tmp = h->old_nonl;
869 h->old_nonl = h->new_nonl;
870 h->new_nonl = tmp;
872 for (i = 0; i < h->len; ++i) {
873 if (*h->lines[i] == '+')
874 *h->lines[i] = '-';
875 else if (*h->lines[i] == '-')
876 *h->lines[i] = '+';
881 const struct got_error *
882 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
883 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
884 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
886 const struct got_error *err = NULL, *complete_err = NULL;
887 struct got_fileindex *fileindex = NULL;
888 char *fileindex_path = NULL;
889 char *oldpath, *newpath;
890 struct imsgbuf *ibuf;
891 int imsg_fds[2] = {-1, -1};
892 int overlapcnt, done = 0, failed = 0;
893 pid_t pid;
895 ibuf = calloc(1, sizeof(*ibuf));
896 if (ibuf == NULL) {
897 err = got_error_from_errno("calloc");
898 goto done;
901 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
902 err = got_error_from_errno("socketpair");
903 goto done;
906 pid = fork();
907 if (pid == -1) {
908 err = got_error_from_errno("fork");
909 goto done;
910 } else if (pid == 0) {
911 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
912 NULL);
913 /* not reached */
916 if (close(imsg_fds[1]) == -1) {
917 err = got_error_from_errno("close");
918 goto done;
920 imsg_fds[1] = -1;
921 imsg_init(ibuf, imsg_fds[0]);
923 err = send_patch(ibuf, fd);
924 fd = -1;
925 if (err)
926 goto done;
928 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
929 worktree);
930 if (err)
931 goto done;
933 while (!done && err == NULL) {
934 struct got_patch p;
935 struct patch_args pa;
937 pa.progress_cb = progress_cb;
938 pa.progress_arg = progress_arg;
939 pa.head = &p.head;
941 err = recv_patch(ibuf, &done, &p, strip);
942 if (err || done)
943 break;
945 if (reverse)
946 reverse_patch(&p);
948 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
949 &newpath, worktree, repo, fileindex);
950 if (err == NULL)
951 err = apply_patch(&overlapcnt, worktree, repo,
952 fileindex, oldpath, newpath, &p, nop, &pa,
953 cancel_cb, cancel_arg);
954 if (err != NULL) {
955 failed = 1;
956 /* recoverable errors */
957 if (err->code == GOT_ERR_FILE_STATUS ||
958 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
959 err = report_progress(&pa, p.old, p.new,
960 GOT_STATUS_CANNOT_UPDATE, err);
961 else if (err->code == GOT_ERR_HUNK_FAILED)
962 err = report_progress(&pa, p.old, p.new,
963 GOT_STATUS_CANNOT_UPDATE, NULL);
965 if (overlapcnt != 0)
966 failed = 1;
968 free(oldpath);
969 free(newpath);
970 patch_free(&p);
972 if (err)
973 break;
976 done:
977 if (fileindex != NULL)
978 complete_err = got_worktree_patch_complete(fileindex,
979 fileindex_path);
980 if (complete_err && err == NULL)
981 err = complete_err;
982 free(fileindex_path);
983 if (fd != -1 && close(fd) == -1 && err == NULL)
984 err = got_error_from_errno("close");
985 if (ibuf != NULL)
986 imsg_clear(ibuf);
987 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
988 err = got_error_from_errno("close");
989 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
990 err = got_error_from_errno("close");
991 if (err == NULL && failed)
992 err = got_error(GOT_ERR_PATCH_FAILED);
993 return err;