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 return got_privsep_flush_imsg(ibuf);
103 static void
104 patch_free(struct got_patch *p)
106 struct got_patch_hunk *h;
107 size_t i;
109 while (!STAILQ_EMPTY(&p->head)) {
110 h = STAILQ_FIRST(&p->head);
111 STAILQ_REMOVE_HEAD(&p->head, entries);
113 for (i = 0; i < h->len; ++i)
114 free(h->lines[i]);
115 free(h->lines);
116 free(h);
119 free(p->new);
120 free(p->old);
122 memset(p, 0, sizeof(*p));
123 STAILQ_INIT(&p->head);
126 static const struct got_error *
127 pushline(struct got_patch_hunk *h, const char *line)
129 void *t;
130 size_t newcap;
132 if (h->len == h->cap) {
133 if ((newcap = h->cap * 1.5) == 0)
134 newcap = 16;
135 t = recallocarray(h->lines, h->cap, newcap,
136 sizeof(h->lines[0]));
137 if (t == NULL)
138 return got_error_from_errno("recallocarray");
139 h->lines = t;
140 h->cap = newcap;
143 if ((t = strdup(line)) == NULL)
144 return got_error_from_errno("strdup");
146 h->lines[h->len++] = t;
147 return NULL;
150 static const struct got_error *
151 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
153 const struct got_error *err = NULL;
154 struct imsg imsg;
155 struct got_imsg_patch_hunk hdr;
156 struct got_imsg_patch patch;
157 struct got_patch_hunk *h = NULL;
158 size_t datalen;
159 int lastmode = -1;
161 memset(p, 0, sizeof(*p));
162 STAILQ_INIT(&p->head);
164 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
165 if (err)
166 return err;
167 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
168 *done = 1;
169 goto done;
171 if (imsg.hdr.type != GOT_IMSG_PATCH) {
172 err = got_error(GOT_ERR_PRIVSEP_MSG);
173 goto done;
175 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
176 if (datalen != sizeof(patch)) {
177 err = got_error(GOT_ERR_PRIVSEP_LEN);
178 goto done;
180 memcpy(&patch, imsg.data, sizeof(patch));
182 if (patch.old[sizeof(patch.old)-1] != '\0' ||
183 patch.new[sizeof(patch.new)-1] != '\0' ||
184 patch.cid[sizeof(patch.cid)-1] != '\0' ||
185 patch.blob[sizeof(patch.blob)-1] != '\0') {
186 err = got_error(GOT_ERR_PRIVSEP_LEN);
187 goto done;
190 if (*patch.cid != '\0')
191 strlcpy(p->cid, patch.cid, sizeof(p->cid));
193 if (*patch.blob != '\0')
194 strlcpy(p->blob, patch.blob, sizeof(p->blob));
196 /* automatically set strip=1 for git-style diffs */
197 if (strip == -1 && patch.git &&
198 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
199 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
200 strip = 1;
202 /* prefer the new name if not /dev/null for not git-style diffs */
203 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
204 err = got_path_strip(&p->old, patch.new, strip);
205 if (err)
206 goto done;
207 } else if (*patch.old != '\0') {
208 err = got_path_strip(&p->old, patch.old, strip);
209 if (err)
210 goto done;
213 if (*patch.new != '\0') {
214 err = got_path_strip(&p->new, patch.new, strip);
215 if (err)
216 goto done;
219 if (p->old == NULL && p->new == NULL) {
220 err = got_error(GOT_ERR_PATCH_MALFORMED);
221 goto done;
224 imsg_free(&imsg);
226 for (;;) {
227 char *t;
229 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
230 if (err) {
231 patch_free(p);
232 return err;
235 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
236 switch (imsg.hdr.type) {
237 case GOT_IMSG_PATCH_DONE:
238 if (h != NULL && h->len == 0)
239 err = got_error(GOT_ERR_PATCH_MALFORMED);
240 goto done;
241 case GOT_IMSG_PATCH_HUNK:
242 if (h != NULL &&
243 (h->len == 0 || h->old_nonl || h->new_nonl)) {
244 err = got_error(GOT_ERR_PATCH_MALFORMED);
245 goto done;
247 lastmode = -1;
248 if (datalen != sizeof(hdr)) {
249 err = got_error(GOT_ERR_PRIVSEP_LEN);
250 goto done;
252 memcpy(&hdr, imsg.data, sizeof(hdr));
253 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
254 err = got_error(GOT_ERR_PRIVSEP_LEN);
255 goto done;
257 if ((h = calloc(1, sizeof(*h))) == NULL) {
258 err = got_error_from_errno("calloc");
259 goto done;
261 h->old_from = hdr.oldfrom;
262 h->old_lines = hdr.oldlines;
263 h->new_from = hdr.newfrom;
264 h->new_lines = hdr.newlines;
265 STAILQ_INSERT_TAIL(&p->head, h, entries);
266 break;
267 case GOT_IMSG_PATCH_LINE:
268 if (h == NULL) {
269 err = got_error(GOT_ERR_PRIVSEP_MSG);
270 goto done;
272 t = imsg.data;
273 /* at least one char */
274 if (datalen < 2 || t[datalen-1] != '\0') {
275 err = got_error(GOT_ERR_PRIVSEP_MSG);
276 goto done;
278 if (*t != ' ' && *t != '-' && *t != '+' &&
279 *t != '\\') {
280 err = got_error(GOT_ERR_PRIVSEP_MSG);
281 goto done;
284 if (*t != '\\')
285 err = pushline(h, t);
286 else if (lastmode == '-')
287 h->old_nonl = 1;
288 else if (lastmode == '+')
289 h->new_nonl = 1;
290 else
291 err = got_error(GOT_ERR_PATCH_MALFORMED);
293 if (err)
294 goto done;
296 lastmode = *t;
297 break;
298 default:
299 err = got_error(GOT_ERR_PRIVSEP_MSG);
300 goto done;
303 imsg_free(&imsg);
306 done:
307 if (err)
308 patch_free(p);
310 imsg_free(&imsg);
311 return err;
314 /*
315 * Copy data from orig starting at copypos until pos into tmp.
316 * If pos is -1, copy until EOF.
317 */
318 static const struct got_error *
319 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
321 char buf[BUFSIZ];
322 size_t len, r, w;
324 if (fseeko(orig, copypos, SEEK_SET) == -1)
325 return got_error_from_errno("fseeko");
327 while (pos == -1 || copypos < pos) {
328 len = sizeof(buf);
329 if (pos > 0)
330 len = MIN(len, (size_t)pos - copypos);
331 r = fread(buf, 1, len, orig);
332 if (r != len && ferror(orig))
333 return got_error_from_errno("fread");
334 w = fwrite(buf, 1, r, tmp);
335 if (w != r)
336 return got_error_from_errno("fwrite");
337 copypos += len;
338 if (r != len && feof(orig)) {
339 if (pos == -1)
340 return NULL;
341 return got_error(GOT_ERR_HUNK_FAILED);
344 return NULL;
347 static const struct got_error *
348 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
350 const struct got_error *err = NULL;
351 char *line = NULL;
352 char mode = *h->lines[0];
353 size_t linesize = 0;
354 ssize_t linelen;
355 off_t match = -1;
356 int match_lineno = -1;
358 for (;;) {
359 linelen = getline(&line, &linesize, orig);
360 if (linelen == -1) {
361 if (ferror(orig))
362 err = got_error_from_errno("getline");
363 else if (match == -1)
364 err = got_error(GOT_ERR_HUNK_FAILED);
365 break;
367 if (line[linelen - 1] == '\n')
368 line[linelen - 1] = '\0';
369 (*lineno)++;
371 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
372 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
373 (mode == '+' && *lineno == h->old_from)) {
374 match = ftello(orig);
375 if (match == -1) {
376 err = got_error_from_errno("ftello");
377 break;
379 match -= linelen;
380 match_lineno = (*lineno)-1;
383 if (*lineno >= h->old_from && match != -1)
384 break;
387 if (err == NULL) {
388 *pos = match;
389 *lineno = match_lineno;
390 if (fseeko(orig, match, SEEK_SET) == -1)
391 err = got_error_from_errno("fseeko");
394 free(line);
395 return err;
398 static int
399 linecmp(const char *a, const char *b, int *mangled)
401 int c;
403 *mangled = 0;
404 c = strcmp(a, b);
405 if (c == 0)
406 return c;
408 *mangled = 1;
409 for (;;) {
410 while (*a == '\t' || *a == ' ' || *a == '\f')
411 a++;
412 while (*b == '\t' || *b == ' ' || *b == '\f')
413 b++;
414 if (*a == '\0' || *a != *b)
415 break;
416 a++, b++;
419 return *a - *b;
422 static const struct got_error *
423 test_hunk(FILE *orig, struct got_patch_hunk *h)
425 const struct got_error *err = NULL;
426 char *line = NULL;
427 size_t linesize = 0, i = 0;
428 ssize_t linelen;
429 int mangled;
431 for (i = 0; i < h->len; ++i) {
432 switch (*h->lines[i]) {
433 case '+':
434 continue;
435 case ' ':
436 case '-':
437 linelen = getline(&line, &linesize, orig);
438 if (linelen == -1) {
439 if (ferror(orig))
440 err = got_error_from_errno("getline");
441 else
442 err = got_error(
443 GOT_ERR_HUNK_FAILED);
444 goto done;
446 if (line[linelen - 1] == '\n')
447 line[linelen - 1] = '\0';
448 if (linecmp(h->lines[i] + 1, line, &mangled)) {
449 err = got_error(GOT_ERR_HUNK_FAILED);
450 goto done;
452 if (mangled)
453 h->ws_mangled = 1;
454 break;
458 done:
459 free(line);
460 return err;
463 static const struct got_error *
464 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
465 off_t from)
467 const struct got_error *err = NULL;
468 const char *t;
469 size_t linesize = 0, i, new = 0;
470 char *line = NULL;
471 char mode;
472 ssize_t linelen;
474 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
475 return got_error_from_errno("fseeko");
477 for (i = 0; i < h->len; ++i) {
478 switch (mode = *h->lines[i]) {
479 case '-':
480 case ' ':
481 (*lineno)++;
482 if (orig != NULL) {
483 linelen = getline(&line, &linesize, orig);
484 if (linelen == -1) {
485 err = got_error_from_errno("getline");
486 goto done;
488 if (line[linelen - 1] == '\n')
489 line[linelen - 1] = '\0';
490 t = line;
491 } else
492 t = h->lines[i] + 1;
493 if (mode == '-')
494 continue;
495 if (fprintf(tmp, "%s\n", t) < 0) {
496 err = got_error_from_errno("fprintf");
497 goto done;
499 break;
500 case '+':
501 new++;
502 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
503 err = got_error_from_errno("fprintf");
504 goto done;
506 if (new != h->new_lines || !h->new_nonl) {
507 if (fprintf(tmp, "\n") < 0) {
508 err = got_error_from_errno("fprintf");
509 goto done;
512 break;
516 done:
517 free(line);
518 return err;
521 static const struct got_error *
522 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
524 const struct got_error *err = NULL;
525 struct got_patch_hunk *h;
526 struct stat sb;
527 int lineno = 0;
528 off_t copypos, pos;
529 char *line = NULL;
530 size_t linesize = 0;
531 ssize_t linelen;
533 if (p->old == NULL) { /* create */
534 h = STAILQ_FIRST(&p->head);
535 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
536 return got_error(GOT_ERR_PATCH_MALFORMED);
537 return apply_hunk(orig, tmp, h, &lineno, 0);
540 if (fstat(fileno(orig), &sb) == -1)
541 return got_error_from_errno("fstat");
543 copypos = 0;
544 STAILQ_FOREACH(h, &p->head, entries) {
545 tryagain:
546 err = locate_hunk(orig, h, &pos, &lineno);
547 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
548 h->err = err;
549 if (err != NULL)
550 return err;
551 err = copy(tmp, orig, copypos, pos);
552 if (err != NULL)
553 return err;
554 copypos = pos;
556 err = test_hunk(orig, h);
557 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
558 /*
559 * try to apply the hunk again starting the search
560 * after the previous partial match.
561 */
562 if (fseeko(orig, pos, SEEK_SET) == -1)
563 return got_error_from_errno("fseeko");
564 linelen = getline(&line, &linesize, orig);
565 if (linelen == -1)
566 return got_error_from_errno("getline");
567 lineno++;
568 goto tryagain;
570 if (err != NULL)
571 return err;
573 if (lineno + 1 != h->old_from)
574 h->offset = lineno + 1 - h->old_from;
576 err = apply_hunk(orig, tmp, h, &lineno, pos);
577 if (err != NULL)
578 return err;
580 copypos = ftello(orig);
581 if (copypos == -1)
582 return got_error_from_errno("ftello");
585 if (p->new == NULL && sb.st_size != copypos) {
586 h = STAILQ_FIRST(&p->head);
587 h->err = got_error(GOT_ERR_HUNK_FAILED);
588 err = h->err;
589 } else if (!feof(orig))
590 err = copy(tmp, orig, copypos, -1);
592 return err;
595 static const struct got_error *
596 report_progress(struct patch_args *pa, const char *old, const char *new,
597 unsigned char status, const struct got_error *orig_error)
599 const struct got_error *err;
600 struct got_patch_hunk *h;
602 err = pa->progress_cb(pa->progress_arg, old, new, status,
603 orig_error, 0, 0, 0, 0, 0, 0, NULL);
604 if (err)
605 return err;
607 STAILQ_FOREACH(h, pa->head, entries) {
608 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
609 continue;
611 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
612 h->old_from, h->old_lines, h->new_from, h->new_lines,
613 h->offset, h->ws_mangled, h->err);
614 if (err)
615 return err;
618 return NULL;
621 static const struct got_error *
622 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
623 const char *path)
625 return report_progress(arg, path, NULL, status, NULL);
628 static const struct got_error *
629 patch_add(void *arg, unsigned char status, const char *path)
631 return report_progress(arg, NULL, path, status, NULL);
634 static const struct got_error *
635 open_blob(char **path, FILE **fp, const char *blobid,
636 struct got_repository *repo)
638 const struct got_error *err = NULL;
639 struct got_blob_object *blob = NULL;
640 struct got_object_id id, *idptr, *matched_id = NULL;
641 int fd = -1;
643 *fp = NULL;
644 *path = NULL;
646 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
647 err = got_repo_match_object_id(&matched_id, NULL, blobid,
648 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
649 repo);
650 if (err)
651 return err;
652 idptr = matched_id;
653 } else {
654 if (!got_parse_sha1_digest(id.sha1, blobid))
655 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
656 idptr = &id;
659 fd = got_opentempfd();
660 if (fd == -1) {
661 err = got_error_from_errno("got_opentempfd");
662 goto done;
665 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
666 if (err)
667 goto done;
669 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
670 if (err)
671 goto done;
673 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
674 if (err)
675 goto done;
677 done:
678 if (fd != -1 && close(fd) == -1 && err == NULL)
679 err = got_error_from_errno("close");
680 if (blob)
681 got_object_blob_close(blob);
682 if (matched_id != NULL)
683 free(matched_id);
684 if (err) {
685 if (*fp != NULL)
686 fclose(*fp);
687 if (*path != NULL)
688 unlink(*path);
689 free(*path);
690 *fp = NULL;
691 *path = NULL;
693 return err;
696 static const struct got_error *
697 apply_patch(int *overlapcnt, struct got_worktree *worktree,
698 struct got_repository *repo, struct got_fileindex *fileindex,
699 const char *old, const char *new, struct got_patch *p, int nop,
700 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
702 const struct got_error *err = NULL;
703 struct stat sb;
704 int do_merge = 0, file_renamed = 0;
705 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
706 char *oldpath = NULL, *newpath = NULL;
707 char *tmppath = NULL, *template = NULL, *parent = NULL;
708 char *apath = NULL, *mergepath = NULL;
709 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
710 int outfd;
711 const char *outpath;
712 mode_t mode = GOT_DEFAULT_FILE_MODE;
714 *overlapcnt = 0;
716 /* don't run the diff3 merge on creations/deletions */
717 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
718 err = open_blob(&apath, &afile, p->blob, repo);
719 /*
720 * ignore failures to open this blob, we might have
721 * parsed gibberish.
722 */
723 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
724 err->code != GOT_ERR_NO_OBJ)
725 return err;
726 else if (err == NULL)
727 do_merge = 1;
728 err = NULL;
731 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
732 old) == -1) {
733 err = got_error_from_errno("asprintf");
734 goto done;
737 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
738 new) == -1) {
739 err = got_error_from_errno("asprintf");
740 goto done;
743 file_renamed = strcmp(oldpath, newpath);
745 if (asprintf(&template, "%s/got-patch",
746 got_worktree_get_root_path(worktree)) == -1) {
747 err = got_error_from_errno(template);
748 goto done;
751 if (p->old != NULL) {
752 if ((oldfile = fopen(oldpath, "r")) == NULL) {
753 err = got_error_from_errno2("open", oldpath);
754 goto done;
756 if (fstat(fileno(oldfile), &sb) == -1) {
757 err = got_error_from_errno2("fstat", oldpath);
758 goto done;
760 mode = sb.st_mode;
763 err = got_opentemp_named(&tmppath, &tmpfile, template);
764 if (err)
765 goto done;
766 outpath = tmppath;
767 outfd = fileno(tmpfile);
768 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
769 if (err)
770 goto done;
772 if (do_merge) {
773 const char *type, *id;
775 if (fseeko(afile, 0, SEEK_SET) == -1 ||
776 fseeko(oldfile, 0, SEEK_SET) == -1 ||
777 fseeko(tmpfile, 0, SEEK_SET) == -1) {
778 err = got_error_from_errno("fseeko");
779 goto done;
782 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
783 err = got_error_from_errno("asprintf");
784 oldlabel = NULL;
785 goto done;
788 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
789 err = got_error_from_errno("asprintf");
790 newlabel = NULL;
791 goto done;
794 if (*p->cid != '\0') {
795 type = "commit";
796 id = p->cid;
797 } else {
798 type = "blob";
799 id = p->blob;
802 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
803 err = got_error_from_errno("asprintf");
804 anclabel = NULL;
805 goto done;
808 err = got_opentemp_named(&mergepath, &mergefile, template);
809 if (err)
810 goto done;
811 outpath = mergepath;
812 outfd = fileno(mergefile);
814 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
815 oldfile, oldlabel, anclabel, newlabel,
816 GOT_DIFF_ALGORITHM_PATIENCE);
817 if (err)
818 goto done;
821 if (nop)
822 goto done;
824 if (p->old != NULL && p->new == NULL) {
825 err = got_worktree_patch_schedule_rm(old, repo, worktree,
826 fileindex, patch_delete, pa);
827 goto done;
830 if (fchmod(outfd, mode) == -1) {
831 err = got_error_from_errno2("chmod", tmppath);
832 goto done;
835 if (rename(outpath, newpath) == -1) {
836 if (errno != ENOENT) {
837 err = got_error_from_errno3("rename", outpath,
838 newpath);
839 goto done;
842 err = got_path_dirname(&parent, newpath);
843 if (err != NULL)
844 goto done;
845 err = got_path_mkdir(parent);
846 if (err != NULL)
847 goto done;
848 if (rename(outpath, newpath) == -1) {
849 err = got_error_from_errno3("rename", outpath,
850 newpath);
851 goto done;
855 if (file_renamed) {
856 err = got_worktree_patch_schedule_rm(old, repo, worktree,
857 fileindex, patch_delete, pa);
858 if (err == NULL)
859 err = got_worktree_patch_schedule_add(new, repo,
860 worktree, fileindex, patch_add,
861 pa);
862 if (err)
863 unlink(newpath);
864 } else if (p->old == NULL) {
865 err = got_worktree_patch_schedule_add(new, repo, worktree,
866 fileindex, patch_add, pa);
867 if (err)
868 unlink(newpath);
869 } else if (*overlapcnt != 0)
870 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
871 else if (do_merge)
872 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
873 else
874 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
876 done:
877 free(parent);
878 free(template);
880 if (tmppath != NULL)
881 unlink(tmppath);
882 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
883 err = got_error_from_errno("fclose");
884 free(tmppath);
886 free(oldpath);
887 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
888 err = got_error_from_errno("fclose");
890 if (apath != NULL)
891 unlink(apath);
892 if (afile != NULL && fclose(afile) == EOF && err == NULL)
893 err = got_error_from_errno("fclose");
894 free(apath);
896 if (mergepath != NULL)
897 unlink(mergepath);
898 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
899 err = got_error_from_errno("fclose");
900 free(mergepath);
902 free(newpath);
903 free(oldlabel);
904 free(newlabel);
905 free(anclabel);
906 return err;
909 static void
910 reverse_patch(struct got_patch *p)
912 struct got_patch_hunk *h;
913 size_t i;
914 int tmp;
916 STAILQ_FOREACH(h, &p->head, entries) {
917 tmp = h->old_from;
918 h->old_from = h->new_from;
919 h->new_from = tmp;
921 tmp = h->old_lines;
922 h->old_lines = h->new_lines;
923 h->new_lines = tmp;
925 tmp = h->old_nonl;
926 h->old_nonl = h->new_nonl;
927 h->new_nonl = tmp;
929 for (i = 0; i < h->len; ++i) {
930 if (*h->lines[i] == '+')
931 *h->lines[i] = '-';
932 else if (*h->lines[i] == '-')
933 *h->lines[i] = '+';
938 const struct got_error *
939 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
940 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
941 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
943 const struct got_error *err = NULL, *complete_err = NULL;
944 struct got_fileindex *fileindex = NULL;
945 char *fileindex_path = NULL;
946 char *oldpath, *newpath;
947 struct imsgbuf *ibuf;
948 int imsg_fds[2] = {-1, -1};
949 int overlapcnt, done = 0, failed = 0;
950 pid_t pid;
952 ibuf = calloc(1, sizeof(*ibuf));
953 if (ibuf == NULL) {
954 err = got_error_from_errno("calloc");
955 goto done;
958 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
959 err = got_error_from_errno("socketpair");
960 goto done;
963 pid = fork();
964 if (pid == -1) {
965 err = got_error_from_errno("fork");
966 goto done;
967 } else if (pid == 0) {
968 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
969 NULL);
970 /* not reached */
973 if (close(imsg_fds[1]) == -1) {
974 err = got_error_from_errno("close");
975 goto done;
977 imsg_fds[1] = -1;
978 imsg_init(ibuf, imsg_fds[0]);
980 err = send_patch(ibuf, fd);
981 fd = -1;
982 if (err)
983 goto done;
985 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
986 worktree);
987 if (err)
988 goto done;
990 while (!done && err == NULL) {
991 struct got_patch p;
992 struct patch_args pa;
994 pa.progress_cb = progress_cb;
995 pa.progress_arg = progress_arg;
996 pa.head = &p.head;
998 err = recv_patch(ibuf, &done, &p, strip);
999 if (err || done)
1000 break;
1002 if (reverse)
1003 reverse_patch(&p);
1005 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1006 &newpath, worktree, repo, fileindex);
1007 if (err == NULL)
1008 err = apply_patch(&overlapcnt, worktree, repo,
1009 fileindex, oldpath, newpath, &p, nop, &pa,
1010 cancel_cb, cancel_arg);
1011 if (err != NULL) {
1012 failed = 1;
1013 /* recoverable errors */
1014 if (err->code == GOT_ERR_FILE_STATUS ||
1015 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1016 err = report_progress(&pa, p.old, p.new,
1017 GOT_STATUS_CANNOT_UPDATE, err);
1018 else if (err->code == GOT_ERR_HUNK_FAILED)
1019 err = report_progress(&pa, p.old, p.new,
1020 GOT_STATUS_CANNOT_UPDATE, NULL);
1022 if (overlapcnt != 0)
1023 failed = 1;
1025 free(oldpath);
1026 free(newpath);
1027 patch_free(&p);
1029 if (err)
1030 break;
1033 done:
1034 if (fileindex != NULL)
1035 complete_err = got_worktree_patch_complete(fileindex,
1036 fileindex_path);
1037 if (complete_err && err == NULL)
1038 err = complete_err;
1039 free(fileindex_path);
1040 if (fd != -1 && close(fd) == -1 && err == NULL)
1041 err = got_error_from_errno("close");
1042 if (ibuf != NULL)
1043 imsg_clear(ibuf);
1044 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1045 err = got_error_from_errno("close");
1046 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1047 err = got_error_from_errno("close");
1048 if (err == NULL && failed)
1049 err = got_error(GOT_ERR_PATCH_FAILED);
1050 return err;