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 <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_reference.h"
42 #include "got_cancel.h"
43 #include "got_worktree.h"
44 #include "got_repository.h"
45 #include "got_opentemp.h"
46 #include "got_patch.h"
47 #include "got_diff.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_diff.h"
51 #include "got_lib_object.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_sha1.h"
55 #define MIN(a, b) ((a) < (b) ? (a) : (b))
57 struct got_patch_hunk {
58 STAILQ_ENTRY(got_patch_hunk) entries;
59 const struct got_error *err;
60 int ws_mangled;
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 return got_privsep_flush_imsg(ibuf);
104 static void
105 patch_free(struct got_patch *p)
107 struct got_patch_hunk *h;
108 size_t i;
110 while (!STAILQ_EMPTY(&p->head)) {
111 h = STAILQ_FIRST(&p->head);
112 STAILQ_REMOVE_HEAD(&p->head, entries);
114 for (i = 0; i < h->len; ++i)
115 free(h->lines[i]);
116 free(h->lines);
117 free(h);
120 free(p->new);
121 free(p->old);
123 memset(p, 0, sizeof(*p));
124 STAILQ_INIT(&p->head);
127 static const struct got_error *
128 pushline(struct got_patch_hunk *h, const char *line)
130 void *t;
131 size_t newcap;
133 if (h->len == h->cap) {
134 if ((newcap = h->cap * 1.5) == 0)
135 newcap = 16;
136 t = recallocarray(h->lines, h->cap, newcap,
137 sizeof(h->lines[0]));
138 if (t == NULL)
139 return got_error_from_errno("recallocarray");
140 h->lines = t;
141 h->cap = newcap;
144 if ((t = strdup(line)) == NULL)
145 return got_error_from_errno("strdup");
147 h->lines[h->len++] = t;
148 return NULL;
151 static const struct got_error *
152 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
154 const struct got_error *err = NULL;
155 struct imsg imsg;
156 struct got_imsg_patch_hunk hdr;
157 struct got_imsg_patch patch;
158 struct got_patch_hunk *h = NULL;
159 size_t datalen;
160 int lastmode = -1;
162 memset(p, 0, sizeof(*p));
163 STAILQ_INIT(&p->head);
165 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
166 if (err)
167 return err;
168 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
169 *done = 1;
170 goto done;
172 if (imsg.hdr.type != GOT_IMSG_PATCH) {
173 err = got_error(GOT_ERR_PRIVSEP_MSG);
174 goto done;
176 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
177 if (datalen != sizeof(patch)) {
178 err = got_error(GOT_ERR_PRIVSEP_LEN);
179 goto done;
181 memcpy(&patch, imsg.data, sizeof(patch));
183 if (patch.old[sizeof(patch.old)-1] != '\0' ||
184 patch.new[sizeof(patch.new)-1] != '\0' ||
185 patch.cid[sizeof(patch.cid)-1] != '\0' ||
186 patch.blob[sizeof(patch.blob)-1] != '\0') {
187 err = got_error(GOT_ERR_PRIVSEP_LEN);
188 goto done;
191 if (*patch.cid != '\0')
192 strlcpy(p->cid, patch.cid, sizeof(p->cid));
194 if (*patch.blob != '\0')
195 strlcpy(p->blob, patch.blob, sizeof(p->blob));
197 /* automatically set strip=1 for git-style diffs */
198 if (strip == -1 && patch.git &&
199 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
200 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
201 strip = 1;
203 /* prefer the new name if not /dev/null for not git-style diffs */
204 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
205 err = got_path_strip(&p->old, patch.new, strip);
206 if (err)
207 goto done;
208 } else if (*patch.old != '\0') {
209 err = got_path_strip(&p->old, patch.old, strip);
210 if (err)
211 goto done;
214 if (*patch.new != '\0') {
215 err = got_path_strip(&p->new, patch.new, strip);
216 if (err)
217 goto done;
220 if (p->old == NULL && p->new == NULL) {
221 err = got_error(GOT_ERR_PATCH_MALFORMED);
222 goto done;
225 imsg_free(&imsg);
227 for (;;) {
228 char *t;
230 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
231 if (err) {
232 patch_free(p);
233 return err;
236 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
237 switch (imsg.hdr.type) {
238 case GOT_IMSG_PATCH_DONE:
239 if (h != NULL && h->len == 0)
240 err = got_error(GOT_ERR_PATCH_MALFORMED);
241 goto done;
242 case GOT_IMSG_PATCH_HUNK:
243 if (h != NULL &&
244 (h->len == 0 || h->old_nonl || h->new_nonl)) {
245 err = got_error(GOT_ERR_PATCH_MALFORMED);
246 goto done;
248 lastmode = -1;
249 if (datalen != sizeof(hdr)) {
250 err = got_error(GOT_ERR_PRIVSEP_LEN);
251 goto done;
253 memcpy(&hdr, imsg.data, sizeof(hdr));
254 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
255 err = got_error(GOT_ERR_PRIVSEP_LEN);
256 goto done;
258 if ((h = calloc(1, sizeof(*h))) == NULL) {
259 err = got_error_from_errno("calloc");
260 goto done;
262 h->old_from = hdr.oldfrom;
263 h->old_lines = hdr.oldlines;
264 h->new_from = hdr.newfrom;
265 h->new_lines = hdr.newlines;
266 STAILQ_INSERT_TAIL(&p->head, h, entries);
267 break;
268 case GOT_IMSG_PATCH_LINE:
269 if (h == NULL) {
270 err = got_error(GOT_ERR_PRIVSEP_MSG);
271 goto done;
273 t = imsg.data;
274 /* at least one char */
275 if (datalen < 2 || t[datalen-1] != '\0') {
276 err = got_error(GOT_ERR_PRIVSEP_MSG);
277 goto done;
279 if (*t != ' ' && *t != '-' && *t != '+' &&
280 *t != '\\') {
281 err = got_error(GOT_ERR_PRIVSEP_MSG);
282 goto done;
285 if (*t != '\\')
286 err = pushline(h, t);
287 else if (lastmode == '-')
288 h->old_nonl = 1;
289 else if (lastmode == '+')
290 h->new_nonl = 1;
291 else
292 err = got_error(GOT_ERR_PATCH_MALFORMED);
294 if (err)
295 goto done;
297 lastmode = *t;
298 break;
299 default:
300 err = got_error(GOT_ERR_PRIVSEP_MSG);
301 goto done;
304 imsg_free(&imsg);
307 done:
308 if (err)
309 patch_free(p);
311 imsg_free(&imsg);
312 return err;
315 /*
316 * Copy data from orig starting at copypos until pos into tmp.
317 * If pos is -1, copy until EOF.
318 */
319 static const struct got_error *
320 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
322 char buf[BUFSIZ];
323 size_t len, r, w;
325 if (fseeko(orig, copypos, SEEK_SET) == -1)
326 return got_error_from_errno("fseeko");
328 while (pos == -1 || copypos < pos) {
329 len = sizeof(buf);
330 if (pos > 0)
331 len = MIN(len, (size_t)pos - copypos);
332 r = fread(buf, 1, len, orig);
333 if (r != len && ferror(orig))
334 return got_error_from_errno("fread");
335 w = fwrite(buf, 1, r, tmp);
336 if (w != r)
337 return got_error_from_errno("fwrite");
338 copypos += len;
339 if (r != len && feof(orig)) {
340 if (pos == -1)
341 return NULL;
342 return got_error(GOT_ERR_HUNK_FAILED);
345 return NULL;
348 static const struct got_error *
349 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
351 const struct got_error *err = NULL;
352 char *line = NULL;
353 char mode = *h->lines[0];
354 size_t linesize = 0;
355 ssize_t linelen;
356 off_t match = -1;
357 int match_lineno = -1;
359 for (;;) {
360 linelen = getline(&line, &linesize, orig);
361 if (linelen == -1) {
362 if (ferror(orig))
363 err = got_error_from_errno("getline");
364 else if (match == -1)
365 err = got_error(GOT_ERR_HUNK_FAILED);
366 break;
368 if (line[linelen - 1] == '\n')
369 line[linelen - 1] = '\0';
370 (*lineno)++;
372 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
373 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
374 (mode == '+' && *lineno == h->old_from)) {
375 match = ftello(orig);
376 if (match == -1) {
377 err = got_error_from_errno("ftello");
378 break;
380 match -= linelen;
381 match_lineno = (*lineno)-1;
384 if (*lineno >= h->old_from && match != -1)
385 break;
388 if (err == NULL) {
389 *pos = match;
390 *lineno = match_lineno;
391 if (fseeko(orig, match, SEEK_SET) == -1)
392 err = got_error_from_errno("fseeko");
395 free(line);
396 return err;
399 static int
400 linecmp(const char *a, const char *b, int *mangled)
402 int c;
404 *mangled = 0;
405 c = strcmp(a, b);
406 if (c == 0)
407 return c;
409 *mangled = 1;
410 for (;;) {
411 while (*a == '\t' || *a == ' ' || *a == '\f')
412 a++;
413 while (*b == '\t' || *b == ' ' || *b == '\f')
414 b++;
415 if (*a == '\0' || *a != *b)
416 break;
417 a++, b++;
420 return *a - *b;
423 static const struct got_error *
424 test_hunk(FILE *orig, struct got_patch_hunk *h)
426 const struct got_error *err = NULL;
427 char *line = NULL;
428 size_t linesize = 0, i = 0;
429 ssize_t linelen;
430 int mangled;
432 for (i = 0; i < h->len; ++i) {
433 switch (*h->lines[i]) {
434 case '+':
435 continue;
436 case ' ':
437 case '-':
438 linelen = getline(&line, &linesize, orig);
439 if (linelen == -1) {
440 if (ferror(orig))
441 err = got_error_from_errno("getline");
442 else
443 err = got_error(
444 GOT_ERR_HUNK_FAILED);
445 goto done;
447 if (line[linelen - 1] == '\n')
448 line[linelen - 1] = '\0';
449 if (linecmp(h->lines[i] + 1, line, &mangled)) {
450 err = got_error(GOT_ERR_HUNK_FAILED);
451 goto done;
453 if (mangled)
454 h->ws_mangled = 1;
455 break;
459 done:
460 free(line);
461 return err;
464 static const struct got_error *
465 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
466 off_t from)
468 const struct got_error *err = NULL;
469 const char *t;
470 size_t linesize = 0, i, new = 0;
471 char *line = NULL;
472 char mode;
473 ssize_t linelen;
475 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
476 return got_error_from_errno("fseeko");
478 for (i = 0; i < h->len; ++i) {
479 switch (mode = *h->lines[i]) {
480 case '-':
481 case ' ':
482 (*lineno)++;
483 if (orig != NULL) {
484 linelen = getline(&line, &linesize, orig);
485 if (linelen == -1) {
486 err = got_error_from_errno("getline");
487 goto done;
489 if (line[linelen - 1] == '\n')
490 line[linelen - 1] = '\0';
491 t = line;
492 } else
493 t = h->lines[i] + 1;
494 if (mode == '-')
495 continue;
496 if (fprintf(tmp, "%s\n", t) < 0) {
497 err = got_error_from_errno("fprintf");
498 goto done;
500 break;
501 case '+':
502 new++;
503 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
504 err = got_error_from_errno("fprintf");
505 goto done;
507 if (new != h->new_lines || !h->new_nonl) {
508 if (fprintf(tmp, "\n") < 0) {
509 err = got_error_from_errno("fprintf");
510 goto done;
513 break;
517 done:
518 free(line);
519 return err;
522 static const struct got_error *
523 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
525 const struct got_error *err = NULL;
526 struct got_patch_hunk *h;
527 struct stat sb;
528 int lineno = 0;
529 off_t copypos, pos;
530 char *line = NULL;
531 size_t linesize = 0;
532 ssize_t linelen;
534 if (p->old == NULL) { /* create */
535 h = STAILQ_FIRST(&p->head);
536 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
537 return got_error(GOT_ERR_PATCH_MALFORMED);
538 return apply_hunk(orig, tmp, h, &lineno, 0);
541 if (fstat(fileno(orig), &sb) == -1)
542 return got_error_from_errno("fstat");
544 copypos = 0;
545 STAILQ_FOREACH(h, &p->head, entries) {
546 tryagain:
547 err = locate_hunk(orig, h, &pos, &lineno);
548 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
549 h->err = err;
550 if (err != NULL)
551 return err;
552 err = copy(tmp, orig, copypos, pos);
553 if (err != NULL)
554 return err;
555 copypos = pos;
557 err = test_hunk(orig, h);
558 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
559 /*
560 * try to apply the hunk again starting the search
561 * after the previous partial match.
562 */
563 if (fseeko(orig, pos, SEEK_SET) == -1)
564 return got_error_from_errno("fseeko");
565 linelen = getline(&line, &linesize, orig);
566 if (linelen == -1)
567 return got_error_from_errno("getline");
568 lineno++;
569 goto tryagain;
571 if (err != NULL)
572 return err;
574 if (lineno + 1 != h->old_from)
575 h->offset = lineno + 1 - h->old_from;
577 err = apply_hunk(orig, tmp, h, &lineno, pos);
578 if (err != NULL)
579 return err;
581 copypos = ftello(orig);
582 if (copypos == -1)
583 return got_error_from_errno("ftello");
586 if (p->new == NULL && sb.st_size != copypos) {
587 h = STAILQ_FIRST(&p->head);
588 h->err = got_error(GOT_ERR_HUNK_FAILED);
589 err = h->err;
590 } else if (!feof(orig))
591 err = copy(tmp, orig, copypos, -1);
593 return err;
596 static const struct got_error *
597 report_progress(struct patch_args *pa, const char *old, const char *new,
598 unsigned char status, const struct got_error *orig_error)
600 const struct got_error *err;
601 struct got_patch_hunk *h;
603 err = pa->progress_cb(pa->progress_arg, old, new, status,
604 orig_error, 0, 0, 0, 0, 0, 0, NULL);
605 if (err)
606 return err;
608 STAILQ_FOREACH(h, pa->head, entries) {
609 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
610 continue;
612 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
613 h->old_from, h->old_lines, h->new_from, h->new_lines,
614 h->offset, h->ws_mangled, h->err);
615 if (err)
616 return err;
619 return NULL;
622 static const struct got_error *
623 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
624 const char *path)
626 return report_progress(arg, path, NULL, status, NULL);
629 static const struct got_error *
630 patch_add(void *arg, unsigned char status, const char *path)
632 return report_progress(arg, NULL, path, status, NULL);
635 static const struct got_error *
636 open_blob(char **path, FILE **fp, const char *blobid,
637 struct got_repository *repo)
639 const struct got_error *err = NULL;
640 struct got_blob_object *blob = NULL;
641 struct got_object_id id, *idptr, *matched_id = NULL;
642 int fd = -1;
644 *fp = NULL;
645 *path = NULL;
647 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
648 err = got_repo_match_object_id(&matched_id, NULL, blobid,
649 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
650 repo);
651 if (err)
652 return err;
653 idptr = matched_id;
654 } else {
655 if (!got_parse_sha1_digest(id.sha1, blobid))
656 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
657 idptr = &id;
660 fd = got_opentempfd();
661 if (fd == -1) {
662 err = got_error_from_errno("got_opentempfd");
663 goto done;
666 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
667 if (err)
668 goto done;
670 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
671 if (err)
672 goto done;
674 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
675 if (err)
676 goto done;
678 done:
679 if (fd != -1 && close(fd) == -1 && err == NULL)
680 err = got_error_from_errno("close");
681 if (blob)
682 got_object_blob_close(blob);
683 if (matched_id != NULL)
684 free(matched_id);
685 if (err) {
686 if (*fp != NULL)
687 fclose(*fp);
688 if (*path != NULL)
689 unlink(*path);
690 free(*path);
691 *fp = NULL;
692 *path = NULL;
694 return err;
697 static const struct got_error *
698 apply_patch(int *overlapcnt, struct got_worktree *worktree,
699 struct got_repository *repo, struct got_fileindex *fileindex,
700 const char *old, const char *new, struct got_patch *p, int nop,
701 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
703 const struct got_error *err = NULL;
704 struct stat sb;
705 int do_merge = 0, file_renamed = 0;
706 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
707 char *oldpath = NULL, *newpath = NULL;
708 char *tmppath = NULL, *template = NULL, *parent = NULL;
709 char *apath = NULL, *mergepath = NULL;
710 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
711 int outfd;
712 const char *outpath;
713 mode_t mode = GOT_DEFAULT_FILE_MODE;
715 *overlapcnt = 0;
717 /* don't run the diff3 merge on creations/deletions */
718 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
719 err = open_blob(&apath, &afile, p->blob, repo);
720 /*
721 * ignore failures to open this blob, we might have
722 * parsed gibberish.
723 */
724 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
725 err->code != GOT_ERR_NO_OBJ)
726 return err;
727 else if (err == NULL)
728 do_merge = 1;
729 err = NULL;
732 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
733 old) == -1) {
734 err = got_error_from_errno("asprintf");
735 goto done;
738 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
739 new) == -1) {
740 err = got_error_from_errno("asprintf");
741 goto done;
744 file_renamed = strcmp(oldpath, newpath);
746 if (asprintf(&template, "%s/got-patch",
747 got_worktree_get_root_path(worktree)) == -1) {
748 err = got_error_from_errno(template);
749 goto done;
752 if (p->old != NULL) {
753 if ((oldfile = fopen(oldpath, "r")) == NULL) {
754 err = got_error_from_errno2("open", oldpath);
755 goto done;
757 if (fstat(fileno(oldfile), &sb) == -1) {
758 err = got_error_from_errno2("fstat", oldpath);
759 goto done;
761 mode = sb.st_mode;
764 err = got_opentemp_named(&tmppath, &tmpfile, template);
765 if (err)
766 goto done;
767 outpath = tmppath;
768 outfd = fileno(tmpfile);
769 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
770 if (err)
771 goto done;
773 if (do_merge) {
774 const char *type, *id;
776 if (fseeko(afile, 0, SEEK_SET) == -1 ||
777 fseeko(oldfile, 0, SEEK_SET) == -1 ||
778 fseeko(tmpfile, 0, SEEK_SET) == -1) {
779 err = got_error_from_errno("fseeko");
780 goto done;
783 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
784 err = got_error_from_errno("asprintf");
785 oldlabel = NULL;
786 goto done;
789 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
790 err = got_error_from_errno("asprintf");
791 newlabel = NULL;
792 goto done;
795 if (*p->cid != '\0') {
796 type = "commit";
797 id = p->cid;
798 } else {
799 type = "blob";
800 id = p->blob;
803 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
804 err = got_error_from_errno("asprintf");
805 anclabel = NULL;
806 goto done;
809 err = got_opentemp_named(&mergepath, &mergefile, template);
810 if (err)
811 goto done;
812 outpath = mergepath;
813 outfd = fileno(mergefile);
815 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
816 oldfile, oldlabel, anclabel, newlabel,
817 GOT_DIFF_ALGORITHM_PATIENCE);
818 if (err)
819 goto done;
822 if (nop)
823 goto done;
825 if (p->old != NULL && p->new == NULL) {
826 err = got_worktree_patch_schedule_rm(old, repo, worktree,
827 fileindex, patch_delete, pa);
828 goto done;
831 if (fchmod(outfd, mode) == -1) {
832 err = got_error_from_errno2("chmod", tmppath);
833 goto done;
836 if (rename(outpath, newpath) == -1) {
837 if (errno != ENOENT) {
838 err = got_error_from_errno3("rename", outpath,
839 newpath);
840 goto done;
843 err = got_path_dirname(&parent, newpath);
844 if (err != NULL)
845 goto done;
846 err = got_path_mkdir(parent);
847 if (err != NULL)
848 goto done;
849 if (rename(outpath, newpath) == -1) {
850 err = got_error_from_errno3("rename", outpath,
851 newpath);
852 goto done;
856 if (file_renamed) {
857 err = got_worktree_patch_schedule_rm(old, repo, worktree,
858 fileindex, patch_delete, pa);
859 if (err == NULL)
860 err = got_worktree_patch_schedule_add(new, repo,
861 worktree, fileindex, patch_add,
862 pa);
863 if (err)
864 unlink(newpath);
865 } else if (p->old == NULL) {
866 err = got_worktree_patch_schedule_add(new, repo, worktree,
867 fileindex, patch_add, pa);
868 if (err)
869 unlink(newpath);
870 } else if (*overlapcnt != 0)
871 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
872 else if (do_merge)
873 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
874 else
875 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
877 done:
878 free(parent);
879 free(template);
881 if (tmppath != NULL)
882 unlink(tmppath);
883 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
884 err = got_error_from_errno("fclose");
885 free(tmppath);
887 free(oldpath);
888 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
889 err = got_error_from_errno("fclose");
891 if (apath != NULL)
892 unlink(apath);
893 if (afile != NULL && fclose(afile) == EOF && err == NULL)
894 err = got_error_from_errno("fclose");
895 free(apath);
897 if (mergepath != NULL)
898 unlink(mergepath);
899 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
900 err = got_error_from_errno("fclose");
901 free(mergepath);
903 free(newpath);
904 free(oldlabel);
905 free(newlabel);
906 free(anclabel);
907 return err;
910 static void
911 reverse_patch(struct got_patch *p)
913 struct got_patch_hunk *h;
914 size_t i;
915 int tmp;
917 STAILQ_FOREACH(h, &p->head, entries) {
918 tmp = h->old_from;
919 h->old_from = h->new_from;
920 h->new_from = tmp;
922 tmp = h->old_lines;
923 h->old_lines = h->new_lines;
924 h->new_lines = tmp;
926 tmp = h->old_nonl;
927 h->old_nonl = h->new_nonl;
928 h->new_nonl = tmp;
930 for (i = 0; i < h->len; ++i) {
931 if (*h->lines[i] == '+')
932 *h->lines[i] = '-';
933 else if (*h->lines[i] == '-')
934 *h->lines[i] = '+';
939 const struct got_error *
940 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
941 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
942 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
944 const struct got_error *err = NULL, *complete_err = NULL;
945 struct got_fileindex *fileindex = NULL;
946 char *fileindex_path = NULL;
947 char *oldpath, *newpath;
948 struct imsgbuf *ibuf;
949 int imsg_fds[2] = {-1, -1};
950 int overlapcnt, done = 0, failed = 0;
951 pid_t pid;
953 ibuf = calloc(1, sizeof(*ibuf));
954 if (ibuf == NULL) {
955 err = got_error_from_errno("calloc");
956 goto done;
959 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
960 err = got_error_from_errno("socketpair");
961 goto done;
964 pid = fork();
965 if (pid == -1) {
966 err = got_error_from_errno("fork");
967 goto done;
968 } else if (pid == 0) {
969 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
970 NULL);
971 /* not reached */
974 if (close(imsg_fds[1]) == -1) {
975 err = got_error_from_errno("close");
976 goto done;
978 imsg_fds[1] = -1;
979 imsg_init(ibuf, imsg_fds[0]);
981 err = send_patch(ibuf, fd);
982 fd = -1;
983 if (err)
984 goto done;
986 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
987 worktree);
988 if (err)
989 goto done;
991 while (!done && err == NULL) {
992 struct got_patch p;
993 struct patch_args pa;
995 pa.progress_cb = progress_cb;
996 pa.progress_arg = progress_arg;
997 pa.head = &p.head;
999 err = recv_patch(ibuf, &done, &p, strip);
1000 if (err || done)
1001 break;
1003 if (reverse)
1004 reverse_patch(&p);
1006 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1007 &newpath, worktree, repo, fileindex);
1008 if (err == NULL)
1009 err = apply_patch(&overlapcnt, worktree, repo,
1010 fileindex, oldpath, newpath, &p, nop, &pa,
1011 cancel_cb, cancel_arg);
1012 if (err != NULL) {
1013 failed = 1;
1014 /* recoverable errors */
1015 if (err->code == GOT_ERR_FILE_STATUS ||
1016 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1017 err = report_progress(&pa, p.old, p.new,
1018 GOT_STATUS_CANNOT_UPDATE, err);
1019 else if (err->code == GOT_ERR_HUNK_FAILED)
1020 err = report_progress(&pa, p.old, p.new,
1021 GOT_STATUS_CANNOT_UPDATE, NULL);
1023 if (overlapcnt != 0)
1024 failed = 1;
1026 free(oldpath);
1027 free(newpath);
1028 patch_free(&p);
1030 if (err)
1031 break;
1034 done:
1035 if (fileindex != NULL)
1036 complete_err = got_worktree_patch_complete(fileindex,
1037 fileindex_path);
1038 if (complete_err && err == NULL)
1039 err = complete_err;
1040 free(fileindex_path);
1041 if (fd != -1 && close(fd) == -1 && err == NULL)
1042 err = got_error_from_errno("close");
1043 if (ibuf != NULL)
1044 imsg_clear(ibuf);
1045 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1046 err = got_error_from_errno("close");
1047 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1048 err = got_error_from_errno("close");
1049 if (err == NULL && failed)
1050 err = got_error(GOT_ERR_PATCH_FAILED);
1051 return err;