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/socket.h>
25 #include <sys/stat.h>
26 #include <sys/uio.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_path.h"
40 #include "got_reference.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_repository.h"
44 #include "got_opentemp.h"
45 #include "got_patch.h"
46 #include "got_diff.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_diff.h"
50 #include "got_lib_object.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_sha1.h"
54 #define MIN(a, b) ((a) < (b) ? (a) : (b))
56 struct got_patch_hunk {
57 STAILQ_ENTRY(got_patch_hunk) entries;
58 const struct got_error *err;
59 int ws_mangled;
60 int offset;
61 int old_nonl;
62 int new_nonl;
63 int old_from;
64 int old_lines;
65 int new_from;
66 int new_lines;
67 size_t len;
68 size_t cap;
69 char **lines;
70 };
72 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
73 struct got_patch {
74 char *old;
75 char *new;
76 char cid[41];
77 char blob[41];
78 struct got_patch_hunk_head head;
79 };
81 struct patch_args {
82 got_patch_progress_cb progress_cb;
83 void *progress_arg;
84 struct got_patch_hunk_head *head;
85 };
87 static const struct got_error *
88 send_patch(struct imsgbuf *ibuf, int fd)
89 {
90 const struct got_error *err = NULL;
92 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
93 NULL, 0) == -1) {
94 err = got_error_from_errno(
95 "imsg_compose GOT_IMSG_PATCH_FILE");
96 close(fd);
97 return err;
98 }
100 if (imsg_flush(ibuf) == -1) {
101 err = got_error_from_errno("imsg_flush");
102 imsg_clear(ibuf);
105 return err;
108 static void
109 patch_free(struct got_patch *p)
111 struct got_patch_hunk *h;
112 size_t i;
114 while (!STAILQ_EMPTY(&p->head)) {
115 h = STAILQ_FIRST(&p->head);
116 STAILQ_REMOVE_HEAD(&p->head, entries);
118 for (i = 0; i < h->len; ++i)
119 free(h->lines[i]);
120 free(h->lines);
121 free(h);
124 free(p->new);
125 free(p->old);
127 memset(p, 0, sizeof(*p));
128 STAILQ_INIT(&p->head);
131 static const struct got_error *
132 pushline(struct got_patch_hunk *h, const char *line)
134 void *t;
135 size_t newcap;
137 if (h->len == h->cap) {
138 if ((newcap = h->cap * 1.5) == 0)
139 newcap = 16;
140 t = recallocarray(h->lines, h->cap, newcap,
141 sizeof(h->lines[0]));
142 if (t == NULL)
143 return got_error_from_errno("recallocarray");
144 h->lines = t;
145 h->cap = newcap;
148 if ((t = strdup(line)) == NULL)
149 return got_error_from_errno("strdup");
151 h->lines[h->len++] = t;
152 return NULL;
155 static const struct got_error *
156 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
158 const struct got_error *err = NULL;
159 struct imsg imsg;
160 struct got_imsg_patch_hunk hdr;
161 struct got_imsg_patch patch;
162 struct got_patch_hunk *h = NULL;
163 size_t datalen;
164 int lastmode = -1;
166 memset(p, 0, sizeof(*p));
167 STAILQ_INIT(&p->head);
169 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
170 if (err)
171 return err;
172 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
173 *done = 1;
174 goto done;
176 if (imsg.hdr.type != GOT_IMSG_PATCH) {
177 err = got_error(GOT_ERR_PRIVSEP_MSG);
178 goto done;
180 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
181 if (datalen != sizeof(patch)) {
182 err = got_error(GOT_ERR_PRIVSEP_LEN);
183 goto done;
185 memcpy(&patch, imsg.data, sizeof(patch));
187 if (patch.old[sizeof(patch.old)-1] != '\0' ||
188 patch.new[sizeof(patch.new)-1] != '\0' ||
189 patch.cid[sizeof(patch.cid)-1] != '\0' ||
190 patch.blob[sizeof(patch.blob)-1] != '\0') {
191 err = got_error(GOT_ERR_PRIVSEP_LEN);
192 goto done;
195 if (*patch.cid != '\0')
196 strlcpy(p->cid, patch.cid, sizeof(p->cid));
198 if (*patch.blob != '\0')
199 strlcpy(p->blob, patch.blob, sizeof(p->blob));
201 /* automatically set strip=1 for git-style diffs */
202 if (strip == -1 && patch.git &&
203 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
204 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
205 strip = 1;
207 /* prefer the new name if not /dev/null for not git-style diffs */
208 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
209 err = got_path_strip(&p->old, patch.new, strip);
210 if (err)
211 goto done;
212 } else if (*patch.old != '\0') {
213 err = got_path_strip(&p->old, patch.old, strip);
214 if (err)
215 goto done;
218 if (*patch.new != '\0') {
219 err = got_path_strip(&p->new, patch.new, strip);
220 if (err)
221 goto done;
224 if (p->old == NULL && p->new == NULL) {
225 err = got_error(GOT_ERR_PATCH_MALFORMED);
226 goto done;
229 imsg_free(&imsg);
231 for (;;) {
232 char *t;
234 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
235 if (err) {
236 patch_free(p);
237 return err;
240 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
241 switch (imsg.hdr.type) {
242 case GOT_IMSG_PATCH_DONE:
243 if (h != NULL && h->len == 0)
244 err = got_error(GOT_ERR_PATCH_MALFORMED);
245 goto done;
246 case GOT_IMSG_PATCH_HUNK:
247 if (h != NULL &&
248 (h->len == 0 || h->old_nonl || h->new_nonl)) {
249 err = got_error(GOT_ERR_PATCH_MALFORMED);
250 goto done;
252 lastmode = -1;
253 if (datalen != sizeof(hdr)) {
254 err = got_error(GOT_ERR_PRIVSEP_LEN);
255 goto done;
257 memcpy(&hdr, imsg.data, sizeof(hdr));
258 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
259 err = got_error(GOT_ERR_PRIVSEP_LEN);
260 goto done;
262 if ((h = calloc(1, sizeof(*h))) == NULL) {
263 err = got_error_from_errno("calloc");
264 goto done;
266 h->old_from = hdr.oldfrom;
267 h->old_lines = hdr.oldlines;
268 h->new_from = hdr.newfrom;
269 h->new_lines = hdr.newlines;
270 STAILQ_INSERT_TAIL(&p->head, h, entries);
271 break;
272 case GOT_IMSG_PATCH_LINE:
273 if (h == NULL) {
274 err = got_error(GOT_ERR_PRIVSEP_MSG);
275 goto done;
277 t = imsg.data;
278 /* at least one char */
279 if (datalen < 2 || t[datalen-1] != '\0') {
280 err = got_error(GOT_ERR_PRIVSEP_MSG);
281 goto done;
283 if (*t != ' ' && *t != '-' && *t != '+' &&
284 *t != '\\') {
285 err = got_error(GOT_ERR_PRIVSEP_MSG);
286 goto done;
289 if (*t != '\\')
290 err = pushline(h, t);
291 else if (lastmode == '-')
292 h->old_nonl = 1;
293 else if (lastmode == '+')
294 h->new_nonl = 1;
295 else
296 err = got_error(GOT_ERR_PATCH_MALFORMED);
298 if (err)
299 goto done;
301 lastmode = *t;
302 break;
303 default:
304 err = got_error(GOT_ERR_PRIVSEP_MSG);
305 goto done;
308 imsg_free(&imsg);
311 done:
312 if (err)
313 patch_free(p);
315 imsg_free(&imsg);
316 return err;
319 /*
320 * Copy data from orig starting at copypos until pos into tmp.
321 * If pos is -1, copy until EOF.
322 */
323 static const struct got_error *
324 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
326 char buf[BUFSIZ];
327 size_t len, r, w;
329 if (fseeko(orig, copypos, SEEK_SET) == -1)
330 return got_error_from_errno("fseeko");
332 while (pos == -1 || copypos < pos) {
333 len = sizeof(buf);
334 if (pos > 0)
335 len = MIN(len, (size_t)pos - copypos);
336 r = fread(buf, 1, len, orig);
337 if (r != len && ferror(orig))
338 return got_error_from_errno("fread");
339 w = fwrite(buf, 1, r, tmp);
340 if (w != r)
341 return got_error_from_errno("fwrite");
342 copypos += len;
343 if (r != len && feof(orig)) {
344 if (pos == -1)
345 return NULL;
346 return got_error(GOT_ERR_HUNK_FAILED);
349 return NULL;
352 static const struct got_error *
353 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
355 const struct got_error *err = NULL;
356 char *line = NULL;
357 char mode = *h->lines[0];
358 size_t linesize = 0;
359 ssize_t linelen;
360 off_t match = -1;
361 int match_lineno = -1;
363 for (;;) {
364 linelen = getline(&line, &linesize, orig);
365 if (linelen == -1) {
366 if (ferror(orig))
367 err = got_error_from_errno("getline");
368 else if (match == -1)
369 err = got_error(GOT_ERR_HUNK_FAILED);
370 break;
372 if (line[linelen - 1] == '\n')
373 line[linelen - 1] = '\0';
374 (*lineno)++;
376 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
377 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
378 (mode == '+' && *lineno == h->old_from)) {
379 match = ftello(orig);
380 if (match == -1) {
381 err = got_error_from_errno("ftello");
382 break;
384 match -= linelen;
385 match_lineno = (*lineno)-1;
388 if (*lineno >= h->old_from && match != -1)
389 break;
392 if (err == NULL) {
393 *pos = match;
394 *lineno = match_lineno;
395 if (fseeko(orig, match, SEEK_SET) == -1)
396 err = got_error_from_errno("fseeko");
399 free(line);
400 return err;
403 static int
404 linecmp(const char *a, const char *b, int *mangled)
406 int c;
408 *mangled = 0;
409 c = strcmp(a, b);
410 if (c == 0)
411 return c;
413 *mangled = 1;
414 for (;;) {
415 while (*a == '\t' || *a == ' ' || *a == '\f')
416 a++;
417 while (*b == '\t' || *b == ' ' || *b == '\f')
418 b++;
419 if (*a == '\0' || *a != *b)
420 break;
421 a++, b++;
424 return *a - *b;
427 static const struct got_error *
428 test_hunk(FILE *orig, struct got_patch_hunk *h)
430 const struct got_error *err = NULL;
431 char *line = NULL;
432 size_t linesize = 0, i = 0;
433 ssize_t linelen;
434 int mangled;
436 for (i = 0; i < h->len; ++i) {
437 switch (*h->lines[i]) {
438 case '+':
439 continue;
440 case ' ':
441 case '-':
442 linelen = getline(&line, &linesize, orig);
443 if (linelen == -1) {
444 if (ferror(orig))
445 err = got_error_from_errno("getline");
446 else
447 err = got_error(
448 GOT_ERR_HUNK_FAILED);
449 goto done;
451 if (line[linelen - 1] == '\n')
452 line[linelen - 1] = '\0';
453 if (linecmp(h->lines[i] + 1, line, &mangled)) {
454 err = got_error(GOT_ERR_HUNK_FAILED);
455 goto done;
457 if (mangled)
458 h->ws_mangled = 1;
459 break;
463 done:
464 free(line);
465 return err;
468 static const struct got_error *
469 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
470 off_t from)
472 const struct got_error *err = NULL;
473 const char *t;
474 size_t linesize = 0, i, new = 0;
475 char *line = NULL;
476 char mode;
477 ssize_t linelen;
479 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
480 return got_error_from_errno("fseeko");
482 for (i = 0; i < h->len; ++i) {
483 switch (mode = *h->lines[i]) {
484 case '-':
485 case ' ':
486 (*lineno)++;
487 if (orig != NULL) {
488 linelen = getline(&line, &linesize, orig);
489 if (linelen == -1) {
490 err = got_error_from_errno("getline");
491 goto done;
493 if (line[linelen - 1] == '\n')
494 line[linelen - 1] = '\0';
495 t = line;
496 } else
497 t = h->lines[i] + 1;
498 if (mode == '-')
499 continue;
500 if (fprintf(tmp, "%s\n", t) < 0) {
501 err = got_error_from_errno("fprintf");
502 goto done;
504 break;
505 case '+':
506 new++;
507 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
508 err = got_error_from_errno("fprintf");
509 goto done;
511 if (new != h->new_lines || !h->new_nonl) {
512 if (fprintf(tmp, "\n") < 0) {
513 err = got_error_from_errno("fprintf");
514 goto done;
517 break;
521 done:
522 free(line);
523 return err;
526 static const struct got_error *
527 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
529 const struct got_error *err = NULL;
530 struct got_patch_hunk *h;
531 struct stat sb;
532 int lineno = 0;
533 off_t copypos, pos;
534 char *line = NULL;
535 size_t linesize = 0;
536 ssize_t linelen;
538 if (p->old == NULL) { /* create */
539 h = STAILQ_FIRST(&p->head);
540 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
541 return got_error(GOT_ERR_PATCH_MALFORMED);
542 return apply_hunk(orig, tmp, h, &lineno, 0);
545 if (fstat(fileno(orig), &sb) == -1)
546 return got_error_from_errno("fstat");
548 copypos = 0;
549 STAILQ_FOREACH(h, &p->head, entries) {
550 tryagain:
551 err = locate_hunk(orig, h, &pos, &lineno);
552 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
553 h->err = err;
554 if (err != NULL)
555 return err;
556 err = copy(tmp, orig, copypos, pos);
557 if (err != NULL)
558 return err;
559 copypos = pos;
561 err = test_hunk(orig, h);
562 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
563 /*
564 * try to apply the hunk again starting the search
565 * after the previous partial match.
566 */
567 if (fseeko(orig, pos, SEEK_SET) == -1)
568 return got_error_from_errno("fseeko");
569 linelen = getline(&line, &linesize, orig);
570 if (linelen == -1)
571 return got_error_from_errno("getline");
572 lineno++;
573 goto tryagain;
575 if (err != NULL)
576 return err;
578 if (lineno + 1 != h->old_from)
579 h->offset = lineno + 1 - h->old_from;
581 err = apply_hunk(orig, tmp, h, &lineno, pos);
582 if (err != NULL)
583 return err;
585 copypos = ftello(orig);
586 if (copypos == -1)
587 return got_error_from_errno("ftello");
590 if (p->new == NULL && sb.st_size != copypos) {
591 h = STAILQ_FIRST(&p->head);
592 h->err = got_error(GOT_ERR_HUNK_FAILED);
593 err = h->err;
594 } else if (!feof(orig))
595 err = copy(tmp, orig, copypos, -1);
597 return err;
600 static const struct got_error *
601 report_progress(struct patch_args *pa, const char *old, const char *new,
602 unsigned char status, const struct got_error *orig_error)
604 const struct got_error *err;
605 struct got_patch_hunk *h;
607 err = pa->progress_cb(pa->progress_arg, old, new, status,
608 orig_error, 0, 0, 0, 0, 0, 0, NULL);
609 if (err)
610 return err;
612 STAILQ_FOREACH(h, pa->head, entries) {
613 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
614 continue;
616 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
617 h->old_from, h->old_lines, h->new_from, h->new_lines,
618 h->offset, h->ws_mangled, h->err);
619 if (err)
620 return err;
623 return NULL;
626 static const struct got_error *
627 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
628 const char *path)
630 return report_progress(arg, path, NULL, status, NULL);
633 static const struct got_error *
634 patch_add(void *arg, unsigned char status, const char *path)
636 return report_progress(arg, NULL, path, status, NULL);
639 static const struct got_error *
640 open_blob(char **path, FILE **fp, const char *blobid,
641 struct got_repository *repo)
643 const struct got_error *err = NULL;
644 struct got_blob_object *blob = NULL;
645 struct got_object_id id, *idptr, *matched_id = NULL;
646 int fd = -1;
648 *fp = NULL;
649 *path = NULL;
651 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
652 err = got_repo_match_object_id(&matched_id, NULL, blobid,
653 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
654 repo);
655 if (err)
656 return err;
657 idptr = matched_id;
658 } else {
659 if (!got_parse_sha1_digest(id.sha1, blobid))
660 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
661 idptr = &id;
664 fd = got_opentempfd();
665 if (fd == -1) {
666 err = got_error_from_errno("got_opentempfd");
667 goto done;
670 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
671 if (err)
672 goto done;
674 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
675 if (err)
676 goto done;
678 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
679 if (err)
680 goto done;
682 done:
683 if (fd != -1 && close(fd) == -1 && err == NULL)
684 err = got_error_from_errno("close");
685 if (blob)
686 got_object_blob_close(blob);
687 if (matched_id != NULL)
688 free(matched_id);
689 if (err) {
690 if (*fp != NULL)
691 fclose(*fp);
692 if (*path != NULL)
693 unlink(*path);
694 free(*path);
695 *fp = NULL;
696 *path = NULL;
698 return err;
701 static const struct got_error *
702 apply_patch(int *overlapcnt, struct got_worktree *worktree,
703 struct got_repository *repo, struct got_fileindex *fileindex,
704 const char *old, const char *new, struct got_patch *p, int nop,
705 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
707 const struct got_error *err = NULL;
708 struct stat sb;
709 int do_merge = 0, file_renamed = 0;
710 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
711 char *oldpath = NULL, *newpath = NULL;
712 char *tmppath = NULL, *template = NULL, *parent = NULL;
713 char *apath = NULL, *mergepath = NULL;
714 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
715 int outfd;
716 const char *outpath;
717 mode_t mode = GOT_DEFAULT_FILE_MODE;
719 *overlapcnt = 0;
721 /* don't run the diff3 merge on creations/deletions */
722 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
723 err = open_blob(&apath, &afile, p->blob, repo);
724 /*
725 * ignore failures to open this blob, we might have
726 * parsed gibberish.
727 */
728 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
729 err->code != GOT_ERR_NO_OBJ)
730 return err;
731 else if (err == NULL)
732 do_merge = 1;
733 err = NULL;
736 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
737 old) == -1) {
738 err = got_error_from_errno("asprintf");
739 goto done;
742 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
743 new) == -1) {
744 err = got_error_from_errno("asprintf");
745 goto done;
748 file_renamed = strcmp(oldpath, newpath);
750 if (asprintf(&template, "%s/got-patch",
751 got_worktree_get_root_path(worktree)) == -1) {
752 err = got_error_from_errno(template);
753 goto done;
756 if (p->old != NULL) {
757 if ((oldfile = fopen(oldpath, "r")) == NULL) {
758 err = got_error_from_errno2("open", oldpath);
759 goto done;
761 if (fstat(fileno(oldfile), &sb) == -1) {
762 err = got_error_from_errno2("fstat", oldpath);
763 goto done;
765 mode = sb.st_mode;
768 err = got_opentemp_named(&tmppath, &tmpfile, template);
769 if (err)
770 goto done;
771 outpath = tmppath;
772 outfd = fileno(tmpfile);
773 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
774 if (err)
775 goto done;
777 if (do_merge) {
778 const char *type, *id;
780 if (fseeko(afile, 0, SEEK_SET) == -1 ||
781 fseeko(oldfile, 0, SEEK_SET) == -1 ||
782 fseeko(tmpfile, 0, SEEK_SET) == -1) {
783 err = got_error_from_errno("fseeko");
784 goto done;
787 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
788 err = got_error_from_errno("asprintf");
789 oldlabel = NULL;
790 goto done;
793 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
794 err = got_error_from_errno("asprintf");
795 newlabel = NULL;
796 goto done;
799 if (*p->cid != '\0') {
800 type = "commit";
801 id = p->cid;
802 } else {
803 type = "blob";
804 id = p->blob;
807 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
808 err = got_error_from_errno("asprintf");
809 anclabel = NULL;
810 goto done;
813 err = got_opentemp_named(&mergepath, &mergefile, template);
814 if (err)
815 goto done;
816 outpath = mergepath;
817 outfd = fileno(mergefile);
819 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
820 oldfile, oldlabel, anclabel, newlabel,
821 GOT_DIFF_ALGORITHM_PATIENCE);
822 if (err)
823 goto done;
826 if (nop)
827 goto done;
829 if (p->old != NULL && p->new == NULL) {
830 err = got_worktree_patch_schedule_rm(old, repo, worktree,
831 fileindex, patch_delete, pa);
832 goto done;
835 if (fchmod(outfd, mode) == -1) {
836 err = got_error_from_errno2("chmod", tmppath);
837 goto done;
840 if (rename(outpath, newpath) == -1) {
841 if (errno != ENOENT) {
842 err = got_error_from_errno3("rename", outpath,
843 newpath);
844 goto done;
847 err = got_path_dirname(&parent, newpath);
848 if (err != NULL)
849 goto done;
850 err = got_path_mkdir(parent);
851 if (err != NULL)
852 goto done;
853 if (rename(outpath, newpath) == -1) {
854 err = got_error_from_errno3("rename", outpath,
855 newpath);
856 goto done;
860 if (file_renamed) {
861 err = got_worktree_patch_schedule_rm(old, repo, worktree,
862 fileindex, patch_delete, pa);
863 if (err == NULL)
864 err = got_worktree_patch_schedule_add(new, repo,
865 worktree, fileindex, patch_add,
866 pa);
867 if (err)
868 unlink(newpath);
869 } else if (p->old == NULL) {
870 err = got_worktree_patch_schedule_add(new, repo, worktree,
871 fileindex, patch_add, pa);
872 if (err)
873 unlink(newpath);
874 } else if (*overlapcnt != 0)
875 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
876 else if (do_merge)
877 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
878 else
879 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
881 done:
882 free(parent);
883 free(template);
885 if (tmppath != NULL)
886 unlink(tmppath);
887 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
888 err = got_error_from_errno("fclose");
889 free(tmppath);
891 free(oldpath);
892 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
893 err = got_error_from_errno("fclose");
895 if (apath != NULL)
896 unlink(apath);
897 if (afile != NULL && fclose(afile) == EOF && err == NULL)
898 err = got_error_from_errno("fclose");
899 free(apath);
901 if (mergepath != NULL)
902 unlink(mergepath);
903 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
904 err = got_error_from_errno("fclose");
905 free(mergepath);
907 free(newpath);
908 free(oldlabel);
909 free(newlabel);
910 free(anclabel);
911 return err;
914 static void
915 reverse_patch(struct got_patch *p)
917 struct got_patch_hunk *h;
918 size_t i;
919 int tmp;
921 STAILQ_FOREACH(h, &p->head, entries) {
922 tmp = h->old_from;
923 h->old_from = h->new_from;
924 h->new_from = tmp;
926 tmp = h->old_lines;
927 h->old_lines = h->new_lines;
928 h->new_lines = tmp;
930 tmp = h->old_nonl;
931 h->old_nonl = h->new_nonl;
932 h->new_nonl = tmp;
934 for (i = 0; i < h->len; ++i) {
935 if (*h->lines[i] == '+')
936 *h->lines[i] = '-';
937 else if (*h->lines[i] == '-')
938 *h->lines[i] = '+';
943 const struct got_error *
944 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
945 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
946 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
948 const struct got_error *err = NULL, *complete_err = NULL;
949 struct got_fileindex *fileindex = NULL;
950 char *fileindex_path = NULL;
951 char *oldpath, *newpath;
952 struct imsgbuf *ibuf;
953 int imsg_fds[2] = {-1, -1};
954 int overlapcnt, done = 0, failed = 0;
955 pid_t pid;
957 ibuf = calloc(1, sizeof(*ibuf));
958 if (ibuf == NULL) {
959 err = got_error_from_errno("calloc");
960 goto done;
963 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
964 err = got_error_from_errno("socketpair");
965 goto done;
968 pid = fork();
969 if (pid == -1) {
970 err = got_error_from_errno("fork");
971 goto done;
972 } else if (pid == 0) {
973 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
974 NULL);
975 /* not reached */
978 if (close(imsg_fds[1]) == -1) {
979 err = got_error_from_errno("close");
980 goto done;
982 imsg_fds[1] = -1;
983 imsg_init(ibuf, imsg_fds[0]);
985 err = send_patch(ibuf, fd);
986 fd = -1;
987 if (err)
988 goto done;
990 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
991 worktree);
992 if (err)
993 goto done;
995 while (!done && err == NULL) {
996 struct got_patch p;
997 struct patch_args pa;
999 pa.progress_cb = progress_cb;
1000 pa.progress_arg = progress_arg;
1001 pa.head = &p.head;
1003 err = recv_patch(ibuf, &done, &p, strip);
1004 if (err || done)
1005 break;
1007 if (reverse)
1008 reverse_patch(&p);
1010 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1011 &newpath, worktree, repo, fileindex);
1012 if (err == NULL)
1013 err = apply_patch(&overlapcnt, worktree, repo,
1014 fileindex, oldpath, newpath, &p, nop, &pa,
1015 cancel_cb, cancel_arg);
1016 if (err != NULL) {
1017 failed = 1;
1018 /* recoverable errors */
1019 if (err->code == GOT_ERR_FILE_STATUS ||
1020 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1021 err = report_progress(&pa, p.old, p.new,
1022 GOT_STATUS_CANNOT_UPDATE, err);
1023 else if (err->code == GOT_ERR_HUNK_FAILED)
1024 err = report_progress(&pa, p.old, p.new,
1025 GOT_STATUS_CANNOT_UPDATE, NULL);
1027 if (overlapcnt != 0)
1028 failed = 1;
1030 free(oldpath);
1031 free(newpath);
1032 patch_free(&p);
1034 if (err)
1035 break;
1038 done:
1039 if (fileindex != NULL)
1040 complete_err = got_worktree_patch_complete(fileindex,
1041 fileindex_path);
1042 if (complete_err && err == NULL)
1043 err = complete_err;
1044 free(fileindex_path);
1045 if (fd != -1 && close(fd) == -1 && err == NULL)
1046 err = got_error_from_errno("close");
1047 if (ibuf != NULL)
1048 imsg_clear(ibuf);
1049 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1050 err = got_error_from_errno("close");
1051 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1052 err = got_error_from_errno("close");
1053 if (err == NULL && failed)
1054 err = got_error(GOT_ERR_PATCH_FAILED);
1055 return err;