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 <ctype.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <sha1.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <imsg.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_path.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"
49 #include "got_diff.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;
62 int ws_mangled;
63 int offset;
64 int old_nonl;
65 int new_nonl;
66 int old_from;
67 int old_lines;
68 int new_from;
69 int new_lines;
70 size_t len;
71 size_t cap;
72 char **lines;
73 };
75 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
76 struct got_patch {
77 char *old;
78 char *new;
79 char cid[41];
80 char blob[41];
81 struct got_patch_hunk_head head;
82 };
84 struct patch_args {
85 got_patch_progress_cb progress_cb;
86 void *progress_arg;
87 struct got_patch_hunk_head *head;
88 };
90 static const struct got_error *
91 send_patch(struct imsgbuf *ibuf, int fd)
92 {
93 const struct got_error *err = NULL;
95 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
96 NULL, 0) == -1) {
97 err = got_error_from_errno(
98 "imsg_compose GOT_IMSG_PATCH_FILE");
99 close(fd);
100 return err;
103 return got_privsep_flush_imsg(ibuf);
106 static void
107 patch_free(struct got_patch *p)
109 struct got_patch_hunk *h;
110 size_t i;
112 while (!STAILQ_EMPTY(&p->head)) {
113 h = STAILQ_FIRST(&p->head);
114 STAILQ_REMOVE_HEAD(&p->head, entries);
116 for (i = 0; i < h->len; ++i)
117 free(h->lines[i]);
118 free(h->lines);
119 free(h);
122 free(p->new);
123 free(p->old);
125 memset(p, 0, sizeof(*p));
126 STAILQ_INIT(&p->head);
129 static const struct got_error *
130 pushline(struct got_patch_hunk *h, const char *line)
132 void *t;
133 size_t newcap;
135 if (h->len == h->cap) {
136 if ((newcap = h->cap * 1.5) == 0)
137 newcap = 16;
138 t = recallocarray(h->lines, h->cap, newcap,
139 sizeof(h->lines[0]));
140 if (t == NULL)
141 return got_error_from_errno("recallocarray");
142 h->lines = t;
143 h->cap = newcap;
146 if ((t = strdup(line)) == NULL)
147 return got_error_from_errno("strdup");
149 h->lines[h->len++] = t;
150 return NULL;
153 static const struct got_error *
154 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
156 const struct got_error *err = NULL;
157 struct imsg imsg;
158 struct got_imsg_patch_hunk hdr;
159 struct got_imsg_patch patch;
160 struct got_patch_hunk *h = NULL;
161 size_t datalen;
162 int lastmode = -1;
164 memset(p, 0, sizeof(*p));
165 STAILQ_INIT(&p->head);
167 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
168 if (err)
169 return err;
170 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
171 *done = 1;
172 goto done;
174 if (imsg.hdr.type != GOT_IMSG_PATCH) {
175 err = got_error(GOT_ERR_PRIVSEP_MSG);
176 goto done;
178 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
179 if (datalen != sizeof(patch)) {
180 err = got_error(GOT_ERR_PRIVSEP_LEN);
181 goto done;
183 memcpy(&patch, imsg.data, sizeof(patch));
185 if (patch.old[sizeof(patch.old)-1] != '\0' ||
186 patch.new[sizeof(patch.new)-1] != '\0' ||
187 patch.cid[sizeof(patch.cid)-1] != '\0' ||
188 patch.blob[sizeof(patch.blob)-1] != '\0') {
189 err = got_error(GOT_ERR_PRIVSEP_LEN);
190 goto done;
193 if (*patch.cid != '\0')
194 strlcpy(p->cid, patch.cid, sizeof(p->cid));
196 if (*patch.blob != '\0')
197 strlcpy(p->blob, patch.blob, sizeof(p->blob));
199 /* automatically set strip=1 for git-style diffs */
200 if (strip == -1 && patch.git &&
201 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
202 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
203 strip = 1;
205 /* prefer the new name if not /dev/null for not git-style diffs */
206 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
207 err = got_path_strip(&p->old, patch.new, strip);
208 if (err)
209 goto done;
210 } else if (*patch.old != '\0') {
211 err = got_path_strip(&p->old, patch.old, strip);
212 if (err)
213 goto done;
216 if (*patch.new != '\0') {
217 err = got_path_strip(&p->new, patch.new, strip);
218 if (err)
219 goto done;
222 if (p->old == NULL && p->new == NULL) {
223 err = got_error(GOT_ERR_PATCH_MALFORMED);
224 goto done;
227 imsg_free(&imsg);
229 for (;;) {
230 char *t;
232 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
233 if (err) {
234 patch_free(p);
235 return err;
238 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
239 switch (imsg.hdr.type) {
240 case GOT_IMSG_PATCH_DONE:
241 if (h != NULL && h->len == 0)
242 err = got_error(GOT_ERR_PATCH_MALFORMED);
243 goto done;
244 case GOT_IMSG_PATCH_HUNK:
245 if (h != NULL &&
246 (h->len == 0 || h->old_nonl || h->new_nonl)) {
247 err = got_error(GOT_ERR_PATCH_MALFORMED);
248 goto done;
250 lastmode = -1;
251 if (datalen != sizeof(hdr)) {
252 err = got_error(GOT_ERR_PRIVSEP_LEN);
253 goto done;
255 memcpy(&hdr, imsg.data, sizeof(hdr));
256 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
257 err = got_error(GOT_ERR_PRIVSEP_LEN);
258 goto done;
260 if ((h = calloc(1, sizeof(*h))) == NULL) {
261 err = got_error_from_errno("calloc");
262 goto done;
264 h->old_from = hdr.oldfrom;
265 h->old_lines = hdr.oldlines;
266 h->new_from = hdr.newfrom;
267 h->new_lines = hdr.newlines;
268 STAILQ_INSERT_TAIL(&p->head, h, entries);
269 break;
270 case GOT_IMSG_PATCH_LINE:
271 if (h == NULL) {
272 err = got_error(GOT_ERR_PRIVSEP_MSG);
273 goto done;
275 t = imsg.data;
276 /* at least one char */
277 if (datalen < 2 || t[datalen-1] != '\0') {
278 err = got_error(GOT_ERR_PRIVSEP_MSG);
279 goto done;
281 if (*t != ' ' && *t != '-' && *t != '+' &&
282 *t != '\\') {
283 err = got_error(GOT_ERR_PRIVSEP_MSG);
284 goto done;
287 if (*t != '\\')
288 err = pushline(h, t);
289 else if (lastmode == '-')
290 h->old_nonl = 1;
291 else if (lastmode == '+')
292 h->new_nonl = 1;
293 else
294 err = got_error(GOT_ERR_PATCH_MALFORMED);
296 if (err)
297 goto done;
299 lastmode = *t;
300 break;
301 default:
302 err = got_error(GOT_ERR_PRIVSEP_MSG);
303 goto done;
306 imsg_free(&imsg);
309 done:
310 if (err)
311 patch_free(p);
313 imsg_free(&imsg);
314 return err;
317 /*
318 * Copy data from orig starting at copypos until pos into tmp.
319 * If pos is -1, copy until EOF.
320 */
321 static const struct got_error *
322 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
324 char buf[BUFSIZ];
325 size_t len, r, w;
327 if (fseeko(orig, copypos, SEEK_SET) == -1)
328 return got_error_from_errno("fseeko");
330 while (pos == -1 || copypos < pos) {
331 len = sizeof(buf);
332 if (pos > 0)
333 len = MIN(len, (size_t)pos - copypos);
334 r = fread(buf, 1, len, orig);
335 if (r != len && ferror(orig))
336 return got_error_from_errno("fread");
337 w = fwrite(buf, 1, r, tmp);
338 if (w != r)
339 return got_error_from_errno("fwrite");
340 copypos += len;
341 if (r != len && feof(orig)) {
342 if (pos == -1)
343 return NULL;
344 return got_error(GOT_ERR_HUNK_FAILED);
347 return NULL;
350 static const struct got_error *
351 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
353 const struct got_error *err = NULL;
354 char *line = NULL;
355 char mode = *h->lines[0];
356 size_t linesize = 0;
357 ssize_t linelen;
358 off_t match = -1;
359 int match_lineno = -1;
361 for (;;) {
362 linelen = getline(&line, &linesize, orig);
363 if (linelen == -1) {
364 if (ferror(orig))
365 err = got_error_from_errno("getline");
366 else if (match == -1)
367 err = got_error(GOT_ERR_HUNK_FAILED);
368 break;
370 if (line[linelen - 1] == '\n')
371 line[linelen - 1] = '\0';
372 (*lineno)++;
374 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
375 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
376 (mode == '+' && *lineno == h->old_from)) {
377 match = ftello(orig);
378 if (match == -1) {
379 err = got_error_from_errno("ftello");
380 break;
382 match -= linelen;
383 match_lineno = (*lineno)-1;
386 if (*lineno >= h->old_from && match != -1)
387 break;
390 if (err == NULL) {
391 *pos = match;
392 *lineno = match_lineno;
393 if (fseeko(orig, match, SEEK_SET) == -1)
394 err = got_error_from_errno("fseeko");
397 free(line);
398 return err;
401 static int
402 linecmp(const char *a, const char *b, int *mangled)
404 int c;
406 *mangled = 0;
407 c = strcmp(a, b);
408 if (c == 0)
409 return c;
411 *mangled = 1;
412 for (;;) {
413 while (*a == '\t' || *a == ' ' || *a == '\f')
414 a++;
415 while (*b == '\t' || *b == ' ' || *b == '\f')
416 b++;
417 if (*a == '\0' || *a != *b)
418 break;
419 a++, b++;
422 return *a - *b;
425 static const struct got_error *
426 test_hunk(FILE *orig, struct got_patch_hunk *h)
428 const struct got_error *err = NULL;
429 char *line = NULL;
430 size_t linesize = 0, i = 0;
431 ssize_t linelen;
432 int mangled;
434 for (i = 0; i < h->len; ++i) {
435 switch (*h->lines[i]) {
436 case '+':
437 continue;
438 case ' ':
439 case '-':
440 linelen = getline(&line, &linesize, orig);
441 if (linelen == -1) {
442 if (ferror(orig))
443 err = got_error_from_errno("getline");
444 else
445 err = got_error(
446 GOT_ERR_HUNK_FAILED);
447 goto done;
449 if (line[linelen - 1] == '\n')
450 line[linelen - 1] = '\0';
451 if (linecmp(h->lines[i] + 1, line, &mangled)) {
452 err = got_error(GOT_ERR_HUNK_FAILED);
453 goto done;
455 if (mangled)
456 h->ws_mangled = 1;
457 break;
461 done:
462 free(line);
463 return err;
466 static const struct got_error *
467 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
468 off_t from)
470 const struct got_error *err = NULL;
471 const char *t;
472 size_t linesize = 0, i, new = 0;
473 char *line = NULL;
474 char mode;
475 ssize_t linelen;
477 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
478 return got_error_from_errno("fseeko");
480 for (i = 0; i < h->len; ++i) {
481 switch (mode = *h->lines[i]) {
482 case '-':
483 case ' ':
484 (*lineno)++;
485 if (orig != NULL) {
486 linelen = getline(&line, &linesize, orig);
487 if (linelen == -1) {
488 err = got_error_from_errno("getline");
489 goto done;
491 if (line[linelen - 1] == '\n')
492 line[linelen - 1] = '\0';
493 t = line;
494 } else
495 t = h->lines[i] + 1;
496 if (mode == '-')
497 continue;
498 if (fprintf(tmp, "%s\n", t) < 0) {
499 err = got_error_from_errno("fprintf");
500 goto done;
502 break;
503 case '+':
504 new++;
505 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
506 err = got_error_from_errno("fprintf");
507 goto done;
509 if (new != h->new_lines || !h->new_nonl) {
510 if (fprintf(tmp, "\n") < 0) {
511 err = got_error_from_errno("fprintf");
512 goto done;
515 break;
519 done:
520 free(line);
521 return err;
524 static const struct got_error *
525 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
527 const struct got_error *err = NULL;
528 struct got_patch_hunk *h;
529 struct stat sb;
530 int lineno = 0;
531 off_t copypos, pos;
532 char *line = NULL;
533 size_t linesize = 0;
534 ssize_t linelen;
536 if (p->old == NULL) { /* create */
537 h = STAILQ_FIRST(&p->head);
538 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
539 return got_error(GOT_ERR_PATCH_MALFORMED);
540 return apply_hunk(orig, tmp, h, &lineno, 0);
543 if (fstat(fileno(orig), &sb) == -1)
544 return got_error_from_errno("fstat");
546 copypos = 0;
547 STAILQ_FOREACH(h, &p->head, entries) {
548 tryagain:
549 err = locate_hunk(orig, h, &pos, &lineno);
550 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
551 h->err = err;
552 if (err != NULL)
553 return err;
554 err = copy(tmp, orig, copypos, pos);
555 if (err != NULL)
556 return err;
557 copypos = pos;
559 err = test_hunk(orig, h);
560 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
561 /*
562 * try to apply the hunk again starting the search
563 * after the previous partial match.
564 */
565 if (fseeko(orig, pos, SEEK_SET) == -1)
566 return got_error_from_errno("fseeko");
567 linelen = getline(&line, &linesize, orig);
568 if (linelen == -1)
569 return got_error_from_errno("getline");
570 lineno++;
571 goto tryagain;
573 if (err != NULL)
574 return err;
576 if (lineno + 1 != h->old_from)
577 h->offset = lineno + 1 - h->old_from;
579 err = apply_hunk(orig, tmp, h, &lineno, pos);
580 if (err != NULL)
581 return err;
583 copypos = ftello(orig);
584 if (copypos == -1)
585 return got_error_from_errno("ftello");
588 if (p->new == NULL && sb.st_size != copypos) {
589 h = STAILQ_FIRST(&p->head);
590 h->err = got_error(GOT_ERR_HUNK_FAILED);
591 err = h->err;
592 } else if (!feof(orig))
593 err = copy(tmp, orig, copypos, -1);
595 return err;
598 static const struct got_error *
599 report_progress(struct patch_args *pa, const char *old, const char *new,
600 unsigned char status, const struct got_error *orig_error)
602 const struct got_error *err;
603 struct got_patch_hunk *h;
605 err = pa->progress_cb(pa->progress_arg, old, new, status,
606 orig_error, 0, 0, 0, 0, 0, 0, NULL);
607 if (err)
608 return err;
610 STAILQ_FOREACH(h, pa->head, entries) {
611 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
612 continue;
614 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
615 h->old_from, h->old_lines, h->new_from, h->new_lines,
616 h->offset, h->ws_mangled, h->err);
617 if (err)
618 return err;
621 return NULL;
624 static const struct got_error *
625 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
626 const char *path)
628 return report_progress(arg, path, NULL, status, NULL);
631 static const struct got_error *
632 patch_add(void *arg, unsigned char status, const char *path)
634 return report_progress(arg, NULL, path, status, NULL);
637 static const struct got_error *
638 open_blob(char **path, FILE **fp, const char *blobid,
639 struct got_repository *repo)
641 const struct got_error *err = NULL;
642 struct got_blob_object *blob = NULL;
643 struct got_object_id id, *idptr, *matched_id = NULL;
644 int fd = -1;
646 *fp = NULL;
647 *path = NULL;
649 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
650 err = got_repo_match_object_id(&matched_id, NULL, blobid,
651 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
652 repo);
653 if (err)
654 return err;
655 idptr = matched_id;
656 } else {
657 if (!got_parse_sha1_digest(id.sha1, blobid))
658 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
659 idptr = &id;
662 fd = got_opentempfd();
663 if (fd == -1) {
664 err = got_error_from_errno("got_opentempfd");
665 goto done;
668 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
669 if (err)
670 goto done;
672 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
673 if (err)
674 goto done;
676 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
677 if (err)
678 goto done;
680 done:
681 if (fd != -1 && close(fd) == -1 && err == NULL)
682 err = got_error_from_errno("close");
683 if (blob)
684 got_object_blob_close(blob);
685 if (matched_id != NULL)
686 free(matched_id);
687 if (err) {
688 if (*fp != NULL)
689 fclose(*fp);
690 if (*path != NULL)
691 unlink(*path);
692 free(*path);
693 *fp = NULL;
694 *path = NULL;
696 return err;
699 static const struct got_error *
700 apply_patch(int *overlapcnt, struct got_worktree *worktree,
701 struct got_repository *repo, struct got_fileindex *fileindex,
702 const char *old, const char *new, struct got_patch *p, int nop,
703 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
705 const struct got_error *err = NULL;
706 struct stat sb;
707 int do_merge = 0, file_renamed = 0;
708 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
709 char *oldpath = NULL, *newpath = NULL;
710 char *tmppath = NULL, *template = NULL, *parent = NULL;
711 char *apath = NULL, *mergepath = NULL;
712 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
713 int outfd;
714 const char *outpath;
715 mode_t mode = GOT_DEFAULT_FILE_MODE;
717 *overlapcnt = 0;
719 /* don't run the diff3 merge on creations/deletions */
720 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
721 err = open_blob(&apath, &afile, p->blob, repo);
722 /*
723 * ignore failures to open this blob, we might have
724 * parsed gibberish.
725 */
726 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
727 err->code != GOT_ERR_NO_OBJ)
728 return err;
729 else if (err == NULL)
730 do_merge = 1;
731 err = NULL;
734 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
735 old) == -1) {
736 err = got_error_from_errno("asprintf");
737 goto done;
740 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
741 new) == -1) {
742 err = got_error_from_errno("asprintf");
743 goto done;
746 file_renamed = strcmp(oldpath, newpath);
748 if (asprintf(&template, "%s/got-patch",
749 got_worktree_get_root_path(worktree)) == -1) {
750 err = got_error_from_errno(template);
751 goto done;
754 if (p->old != NULL) {
755 if ((oldfile = fopen(oldpath, "r")) == NULL) {
756 err = got_error_from_errno2("open", oldpath);
757 goto done;
759 if (fstat(fileno(oldfile), &sb) == -1) {
760 err = got_error_from_errno2("fstat", oldpath);
761 goto done;
763 mode = sb.st_mode;
766 err = got_opentemp_named(&tmppath, &tmpfile, template);
767 if (err)
768 goto done;
769 outpath = tmppath;
770 outfd = fileno(tmpfile);
771 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
772 if (err)
773 goto done;
775 if (do_merge) {
776 const char *type, *id;
778 if (fseeko(afile, 0, SEEK_SET) == -1 ||
779 fseeko(oldfile, 0, SEEK_SET) == -1 ||
780 fseeko(tmpfile, 0, SEEK_SET) == -1) {
781 err = got_error_from_errno("fseeko");
782 goto done;
785 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
786 err = got_error_from_errno("asprintf");
787 oldlabel = NULL;
788 goto done;
791 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
792 err = got_error_from_errno("asprintf");
793 newlabel = NULL;
794 goto done;
797 if (*p->cid != '\0') {
798 type = "commit";
799 id = p->cid;
800 } else {
801 type = "blob";
802 id = p->blob;
805 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
806 err = got_error_from_errno("asprintf");
807 anclabel = NULL;
808 goto done;
811 err = got_opentemp_named(&mergepath, &mergefile, template);
812 if (err)
813 goto done;
814 outpath = mergepath;
815 outfd = fileno(mergefile);
817 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
818 oldfile, oldlabel, anclabel, newlabel,
819 GOT_DIFF_ALGORITHM_PATIENCE);
820 if (err)
821 goto done;
824 if (nop)
825 goto done;
827 if (p->old != NULL && p->new == NULL) {
828 err = got_worktree_patch_schedule_rm(old, repo, worktree,
829 fileindex, patch_delete, pa);
830 goto done;
833 if (fchmod(outfd, mode) == -1) {
834 err = got_error_from_errno2("chmod", tmppath);
835 goto done;
838 if (rename(outpath, newpath) == -1) {
839 if (errno != ENOENT) {
840 err = got_error_from_errno3("rename", outpath,
841 newpath);
842 goto done;
845 err = got_path_dirname(&parent, newpath);
846 if (err != NULL)
847 goto done;
848 err = got_path_mkdir(parent);
849 if (err != NULL)
850 goto done;
851 if (rename(outpath, newpath) == -1) {
852 err = got_error_from_errno3("rename", outpath,
853 newpath);
854 goto done;
858 if (file_renamed) {
859 err = got_worktree_patch_schedule_rm(old, repo, worktree,
860 fileindex, patch_delete, pa);
861 if (err == NULL)
862 err = got_worktree_patch_schedule_add(new, repo,
863 worktree, fileindex, patch_add,
864 pa);
865 if (err)
866 unlink(newpath);
867 } else if (p->old == NULL) {
868 err = got_worktree_patch_schedule_add(new, repo, worktree,
869 fileindex, patch_add, pa);
870 if (err)
871 unlink(newpath);
872 } else if (*overlapcnt != 0)
873 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
874 else if (do_merge)
875 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
876 else
877 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
879 done:
880 free(parent);
881 free(template);
883 if (tmppath != NULL)
884 unlink(tmppath);
885 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
886 err = got_error_from_errno("fclose");
887 free(tmppath);
889 free(oldpath);
890 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
891 err = got_error_from_errno("fclose");
893 if (apath != NULL)
894 unlink(apath);
895 if (afile != NULL && fclose(afile) == EOF && err == NULL)
896 err = got_error_from_errno("fclose");
897 free(apath);
899 if (mergepath != NULL)
900 unlink(mergepath);
901 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
902 err = got_error_from_errno("fclose");
903 free(mergepath);
905 free(newpath);
906 free(oldlabel);
907 free(newlabel);
908 free(anclabel);
909 return err;
912 static void
913 reverse_patch(struct got_patch *p)
915 struct got_patch_hunk *h;
916 size_t i;
917 int tmp;
919 STAILQ_FOREACH(h, &p->head, entries) {
920 tmp = h->old_from;
921 h->old_from = h->new_from;
922 h->new_from = tmp;
924 tmp = h->old_lines;
925 h->old_lines = h->new_lines;
926 h->new_lines = tmp;
928 tmp = h->old_nonl;
929 h->old_nonl = h->new_nonl;
930 h->new_nonl = tmp;
932 for (i = 0; i < h->len; ++i) {
933 if (*h->lines[i] == '+')
934 *h->lines[i] = '-';
935 else if (*h->lines[i] == '-')
936 *h->lines[i] = '+';
941 const struct got_error *
942 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
943 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
944 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
946 const struct got_error *err = NULL, *complete_err = NULL;
947 struct got_fileindex *fileindex = NULL;
948 char *fileindex_path = NULL;
949 char *oldpath, *newpath;
950 struct imsgbuf *ibuf;
951 int imsg_fds[2] = {-1, -1};
952 int overlapcnt, done = 0, failed = 0;
953 pid_t pid;
955 ibuf = calloc(1, sizeof(*ibuf));
956 if (ibuf == NULL) {
957 err = got_error_from_errno("calloc");
958 goto done;
961 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
962 err = got_error_from_errno("socketpair");
963 goto done;
966 pid = fork();
967 if (pid == -1) {
968 err = got_error_from_errno("fork");
969 goto done;
970 } else if (pid == 0) {
971 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
972 NULL);
973 /* not reached */
976 if (close(imsg_fds[1]) == -1) {
977 err = got_error_from_errno("close");
978 goto done;
980 imsg_fds[1] = -1;
981 imsg_init(ibuf, imsg_fds[0]);
983 err = send_patch(ibuf, fd);
984 fd = -1;
985 if (err)
986 goto done;
988 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
989 worktree);
990 if (err)
991 goto done;
993 while (!done && err == NULL) {
994 struct got_patch p;
995 struct patch_args pa;
997 pa.progress_cb = progress_cb;
998 pa.progress_arg = progress_arg;
999 pa.head = &p.head;
1001 err = recv_patch(ibuf, &done, &p, strip);
1002 if (err || done)
1003 break;
1005 if (reverse)
1006 reverse_patch(&p);
1008 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1009 &newpath, worktree, repo, fileindex);
1010 if (err == NULL)
1011 err = apply_patch(&overlapcnt, worktree, repo,
1012 fileindex, oldpath, newpath, &p, nop, &pa,
1013 cancel_cb, cancel_arg);
1014 if (err != NULL) {
1015 failed = 1;
1016 /* recoverable errors */
1017 if (err->code == GOT_ERR_FILE_STATUS ||
1018 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1019 err = report_progress(&pa, p.old, p.new,
1020 GOT_STATUS_CANNOT_UPDATE, err);
1021 else if (err->code == GOT_ERR_HUNK_FAILED)
1022 err = report_progress(&pa, p.old, p.new,
1023 GOT_STATUS_CANNOT_UPDATE, NULL);
1025 if (overlapcnt != 0)
1026 failed = 1;
1028 free(oldpath);
1029 free(newpath);
1030 patch_free(&p);
1032 if (err)
1033 break;
1036 done:
1037 if (fileindex != NULL)
1038 complete_err = got_worktree_patch_complete(fileindex,
1039 fileindex_path);
1040 if (complete_err && err == NULL)
1041 err = complete_err;
1042 free(fileindex_path);
1043 if (fd != -1 && close(fd) == -1 && err == NULL)
1044 err = got_error_from_errno("close");
1045 if (ibuf != NULL)
1046 imsg_clear(ibuf);
1047 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1048 err = got_error_from_errno("close");
1049 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1050 err = got_error_from_errno("close");
1051 if (err == NULL && failed)
1052 err = got_error(GOT_ERR_PATCH_FAILED);
1053 return err;