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 <errno.h>
29 #include <limits.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_path.h"
39 #include "got_reference.h"
40 #include "got_cancel.h"
41 #include "got_worktree.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
44 #include "got_patch.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_diff.h"
48 #include "got_lib_object.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_sha1.h"
52 #define MIN(a, b) ((a) < (b) ? (a) : (b))
54 struct got_patch_hunk {
55 STAILQ_ENTRY(got_patch_hunk) entries;
56 const struct got_error *err;
57 int offset;
58 int old_nonl;
59 int new_nonl;
60 int old_from;
61 int old_lines;
62 int new_from;
63 int new_lines;
64 size_t len;
65 size_t cap;
66 char **lines;
67 };
69 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
70 struct got_patch {
71 char *old;
72 char *new;
73 char cid[41];
74 char blob[41];
75 struct got_patch_hunk_head head;
76 };
78 struct patch_args {
79 got_patch_progress_cb progress_cb;
80 void *progress_arg;
81 struct got_patch_hunk_head *head;
82 };
84 static const struct got_error *
85 send_patch(struct imsgbuf *ibuf, int fd)
86 {
87 const struct got_error *err = NULL;
89 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
90 NULL, 0) == -1) {
91 err = got_error_from_errno(
92 "imsg_compose GOT_IMSG_PATCH_FILE");
93 close(fd);
94 return err;
95 }
97 if (imsg_flush(ibuf) == -1) {
98 err = got_error_from_errno("imsg_flush");
99 imsg_clear(ibuf);
102 return err;
105 static void
106 patch_free(struct got_patch *p)
108 struct got_patch_hunk *h;
109 size_t i;
111 while (!STAILQ_EMPTY(&p->head)) {
112 h = STAILQ_FIRST(&p->head);
113 STAILQ_REMOVE_HEAD(&p->head, entries);
115 for (i = 0; i < h->len; ++i)
116 free(h->lines[i]);
117 free(h->lines);
118 free(h);
121 free(p->new);
122 free(p->old);
124 memset(p, 0, sizeof(*p));
125 STAILQ_INIT(&p->head);
128 static const struct got_error *
129 pushline(struct got_patch_hunk *h, const char *line)
131 void *t;
132 size_t newcap;
134 if (h->len == h->cap) {
135 if ((newcap = h->cap * 1.5) == 0)
136 newcap = 16;
137 t = recallocarray(h->lines, h->cap, newcap,
138 sizeof(h->lines[0]));
139 if (t == NULL)
140 return got_error_from_errno("recallocarray");
141 h->lines = t;
142 h->cap = newcap;
145 if ((t = strdup(line)) == NULL)
146 return got_error_from_errno("strdup");
148 h->lines[h->len++] = t;
149 return NULL;
152 static const struct got_error *
153 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
155 const struct got_error *err = NULL;
156 struct imsg imsg;
157 struct got_imsg_patch_hunk hdr;
158 struct got_imsg_patch patch;
159 struct got_patch_hunk *h = NULL;
160 size_t datalen;
161 int lastmode = -1;
163 memset(p, 0, sizeof(*p));
164 STAILQ_INIT(&p->head);
166 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
167 if (err)
168 return err;
169 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
170 *done = 1;
171 goto done;
173 if (imsg.hdr.type != GOT_IMSG_PATCH) {
174 err = got_error(GOT_ERR_PRIVSEP_MSG);
175 goto done;
177 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
178 if (datalen != sizeof(patch)) {
179 err = got_error(GOT_ERR_PRIVSEP_LEN);
180 goto done;
182 memcpy(&patch, imsg.data, sizeof(patch));
184 if (patch.old[sizeof(patch.old)-1] != '\0' ||
185 patch.new[sizeof(patch.new)-1] != '\0' ||
186 patch.cid[sizeof(patch.cid)-1] != '\0' ||
187 patch.blob[sizeof(patch.blob)-1] != '\0') {
188 err = got_error(GOT_ERR_PRIVSEP_LEN);
189 goto done;
192 if (*patch.cid != '\0' && *patch.blob != '\0') {
193 strlcpy(p->cid, patch.cid, sizeof(p->cid));
194 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 const struct got_error *
400 test_hunk(FILE *orig, struct got_patch_hunk *h)
402 const struct got_error *err = NULL;
403 char *line = NULL;
404 size_t linesize = 0, i = 0;
405 ssize_t linelen;
407 for (i = 0; i < h->len; ++i) {
408 switch (*h->lines[i]) {
409 case '+':
410 continue;
411 case ' ':
412 case '-':
413 linelen = getline(&line, &linesize, orig);
414 if (linelen == -1) {
415 if (ferror(orig))
416 err = got_error_from_errno("getline");
417 else
418 err = got_error(
419 GOT_ERR_HUNK_FAILED);
420 goto done;
422 if (line[linelen - 1] == '\n')
423 line[linelen - 1] = '\0';
424 if (strcmp(h->lines[i] + 1, line)) {
425 err = got_error(GOT_ERR_HUNK_FAILED);
426 goto done;
428 break;
432 done:
433 free(line);
434 return err;
437 static const struct got_error *
438 apply_hunk(FILE *tmp, struct got_patch_hunk *h, int *lineno)
440 size_t i, new = 0;
442 for (i = 0; i < h->len; ++i) {
443 switch (*h->lines[i]) {
444 case ' ':
445 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
446 return got_error_from_errno("fprintf");
447 /* fallthrough */
448 case '-':
449 (*lineno)++;
450 break;
451 case '+':
452 new++;
453 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
454 return got_error_from_errno("fprintf");
455 if (new != h->new_lines || !h->new_nonl) {
456 if (fprintf(tmp, "\n") < 0)
457 return got_error_from_errno(
458 "fprintf");
460 break;
463 return NULL;
466 static const struct got_error *
467 patch_file(struct got_patch *p, FILE *orig, FILE *tmp, mode_t *mode)
469 const struct got_error *err = NULL;
470 struct got_patch_hunk *h;
471 struct stat sb;
472 int lineno = 0;
473 off_t copypos, pos;
474 char *line = NULL;
475 size_t linesize = 0;
476 ssize_t linelen;
478 if (p->old == NULL) { /* create */
479 h = STAILQ_FIRST(&p->head);
480 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
481 return got_error(GOT_ERR_PATCH_MALFORMED);
482 return apply_hunk(tmp, h, &lineno);
485 if (fstat(fileno(orig), &sb) == -1)
486 return got_error_from_errno("fstat");
487 *mode = sb.st_mode;
489 copypos = 0;
490 STAILQ_FOREACH(h, &p->head, entries) {
491 tryagain:
492 err = locate_hunk(orig, h, &pos, &lineno);
493 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
494 h->err = err;
495 if (err != NULL)
496 return err;
497 err = copy(tmp, orig, copypos, pos);
498 if (err != NULL)
499 return err;
500 copypos = pos;
502 err = test_hunk(orig, h);
503 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
504 /*
505 * try to apply the hunk again starting the search
506 * after the previous partial match.
507 */
508 if (fseeko(orig, pos, SEEK_SET) == -1)
509 return got_error_from_errno("fseeko");
510 linelen = getline(&line, &linesize, orig);
511 if (linelen == -1)
512 return got_error_from_errno("getline");
513 lineno++;
514 goto tryagain;
516 if (err != NULL)
517 return err;
519 if (lineno + 1 != h->old_from)
520 h->offset = lineno + 1 - h->old_from;
522 err = apply_hunk(tmp, h, &lineno);
523 if (err != NULL)
524 return err;
526 copypos = ftello(orig);
527 if (copypos == -1)
528 return got_error_from_errno("ftello");
531 if (p->new == NULL && sb.st_size != copypos) {
532 h = STAILQ_FIRST(&p->head);
533 h->err = got_error(GOT_ERR_HUNK_FAILED);
534 err = h->err;
535 } else if (!feof(orig))
536 err = copy(tmp, orig, copypos, -1);
538 return err;
541 static const struct got_error *
542 report_progress(struct patch_args *pa, const char *old, const char *new,
543 unsigned char status, const struct got_error *orig_error)
545 const struct got_error *err;
546 struct got_patch_hunk *h;
548 err = pa->progress_cb(pa->progress_arg, old, new, status,
549 orig_error, 0, 0, 0, 0, 0, NULL);
550 if (err)
551 return err;
553 STAILQ_FOREACH(h, pa->head, entries) {
554 if (h->offset == 0 && h->err == NULL)
555 continue;
557 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
558 h->old_from, h->old_lines, h->new_from, h->new_lines,
559 h->offset, h->err);
560 if (err)
561 return err;
564 return NULL;
567 static const struct got_error *
568 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
569 const char *path)
571 return report_progress(arg, path, NULL, status, NULL);
574 static const struct got_error *
575 patch_add(void *arg, unsigned char status, const char *path)
577 return report_progress(arg, NULL, path, status, NULL);
580 static const struct got_error *
581 open_blob(char **path, FILE **fp, const char *blobid,
582 struct got_repository *repo)
584 const struct got_error *err = NULL;
585 struct got_blob_object *blob = NULL;
586 struct got_object_id id;
588 *fp = NULL;
589 *path = NULL;
591 if (!got_parse_sha1_digest(id.sha1, blobid))
592 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
594 err = got_object_open_as_blob(&blob, repo, &id, 8192);
595 if (err)
596 goto done;
598 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
599 if (err)
600 goto done;
602 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
603 if (err)
604 goto done;
606 done:
607 if (blob)
608 got_object_blob_close(blob);
609 if (err) {
610 if (*fp != NULL)
611 fclose(*fp);
612 if (*path != NULL)
613 unlink(*path);
614 free(*path);
615 *fp = NULL;
616 *path = NULL;
618 return err;
621 static const struct got_error *
622 apply_patch(int *overlapcnt, struct got_worktree *worktree,
623 struct got_repository *repo, struct got_fileindex *fileindex,
624 const char *old, const char *new, struct got_patch *p, int nop,
625 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
627 const struct got_error *err = NULL;
628 int do_merge = 0, file_renamed = 0;
629 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
630 char *oldpath = NULL, *newpath = NULL;
631 char *tmppath = NULL, *template = NULL, *parent = NULL;
632 char *apath = NULL, *mergepath = NULL;
633 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
634 int outfd;
635 const char *outpath;
636 mode_t mode = GOT_DEFAULT_FILE_MODE;
638 *overlapcnt = 0;
640 /* don't run the diff3 merge on creations/deletions */
641 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
642 err = open_blob(&apath, &afile, p->blob, repo);
643 /*
644 * ignore failures to open this blob, we might have
645 * parsed gibberish.
646 */
647 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
648 return err;
649 else if (err == NULL)
650 do_merge = 1;
651 err = NULL;
654 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
655 old) == -1) {
656 err = got_error_from_errno("asprintf");
657 goto done;
660 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
661 new) == -1) {
662 err = got_error_from_errno("asprintf");
663 goto done;
666 file_renamed = strcmp(oldpath, newpath);
668 if (asprintf(&template, "%s/got-patch",
669 got_worktree_get_root_path(worktree)) == -1) {
670 err = got_error_from_errno(template);
671 goto done;
674 if (p->old != NULL && (oldfile = fopen(oldpath, "r")) == NULL) {
675 err = got_error_from_errno2("open", oldpath);
676 goto done;
679 err = got_opentemp_named(&tmppath, &tmpfile, template);
680 if (err)
681 goto done;
682 outpath = tmppath;
683 outfd = fileno(tmpfile);
684 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile, &mode);
685 if (err)
686 goto done;
688 if (do_merge) {
689 if (fseeko(afile, 0, SEEK_SET) == -1 ||
690 fseeko(oldfile, 0, SEEK_SET) == -1 ||
691 fseeko(tmpfile, 0, SEEK_SET) == -1) {
692 err = got_error_from_errno("fseeko");
693 goto done;
696 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
697 err = got_error_from_errno("asprintf");
698 oldlabel = NULL;
699 goto done;
702 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
703 err = got_error_from_errno("asprintf");
704 newlabel = NULL;
705 goto done;
708 if (asprintf(&anclabel, "commit %s", p->cid) == -1) {
709 err = got_error_from_errno("asprintf");
710 anclabel = NULL;
711 goto done;
714 err = got_opentemp_named(&mergepath, &mergefile, template);
715 if (err)
716 goto done;
717 outpath = mergepath;
718 outfd = fileno(mergefile);
720 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
721 oldfile, oldlabel, anclabel, newlabel,
722 GOT_DIFF_ALGORITHM_PATIENCE);
723 if (err)
724 goto done;
727 if (nop)
728 goto done;
730 if (p->old != NULL && p->new == NULL) {
731 err = got_worktree_patch_schedule_rm(old, repo, worktree,
732 fileindex, patch_delete, pa);
733 goto done;
736 if (fchmod(outfd, mode) == -1) {
737 err = got_error_from_errno2("chmod", tmppath);
738 goto done;
741 if (rename(outpath, newpath) == -1) {
742 if (errno != ENOENT) {
743 err = got_error_from_errno3("rename", outpath,
744 newpath);
745 goto done;
748 err = got_path_dirname(&parent, newpath);
749 if (err != NULL)
750 goto done;
751 err = got_path_mkdir(parent);
752 if (err != NULL)
753 goto done;
754 if (rename(outpath, newpath) == -1) {
755 err = got_error_from_errno3("rename", outpath,
756 newpath);
757 goto done;
761 if (file_renamed) {
762 err = got_worktree_patch_schedule_rm(old, repo, worktree,
763 fileindex, patch_delete, pa);
764 if (err == NULL)
765 err = got_worktree_patch_schedule_add(new, repo,
766 worktree, fileindex, patch_add,
767 pa);
768 if (err)
769 unlink(newpath);
770 } else if (p->old == NULL) {
771 err = got_worktree_patch_schedule_add(new, repo, worktree,
772 fileindex, patch_add, pa);
773 if (err)
774 unlink(newpath);
775 } else if (*overlapcnt != 0)
776 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
777 else
778 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
780 done:
781 free(parent);
782 free(template);
784 if (tmppath != NULL)
785 unlink(tmppath);
786 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
787 err = got_error_from_errno("fclose");
788 free(tmppath);
790 free(oldpath);
791 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
792 err = got_error_from_errno("fclose");
794 if (apath != NULL)
795 unlink(apath);
796 if (afile != NULL && fclose(afile) == EOF && err == NULL)
797 err = got_error_from_errno("fclose");
798 free(apath);
800 if (mergepath != NULL)
801 unlink(mergepath);
802 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
803 err = got_error_from_errno("fclose");
804 free(mergepath);
806 free(newpath);
807 free(oldlabel);
808 free(newlabel);
809 free(anclabel);
810 return err;
813 static void
814 reverse_patch(struct got_patch *p)
816 struct got_patch_hunk *h;
817 size_t i;
818 int tmp;
820 STAILQ_FOREACH(h, &p->head, entries) {
821 tmp = h->old_from;
822 h->old_from = h->new_from;
823 h->new_from = tmp;
825 tmp = h->old_lines;
826 h->old_lines = h->new_lines;
827 h->new_lines = tmp;
829 tmp = h->old_nonl;
830 h->old_nonl = h->new_nonl;
831 h->new_nonl = tmp;
833 for (i = 0; i < h->len; ++i) {
834 if (*h->lines[i] == '+')
835 *h->lines[i] = '-';
836 else if (*h->lines[i] == '-')
837 *h->lines[i] = '+';
842 const struct got_error *
843 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
844 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
845 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
847 const struct got_error *err = NULL, *complete_err = NULL;
848 struct got_fileindex *fileindex = NULL;
849 char *fileindex_path = NULL;
850 char *oldpath, *newpath;
851 struct imsgbuf *ibuf;
852 int imsg_fds[2] = {-1, -1};
853 int overlapcnt, done = 0, failed = 0;
854 pid_t pid;
856 ibuf = calloc(1, sizeof(*ibuf));
857 if (ibuf == NULL) {
858 err = got_error_from_errno("calloc");
859 goto done;
862 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
863 err = got_error_from_errno("socketpair");
864 goto done;
867 pid = fork();
868 if (pid == -1) {
869 err = got_error_from_errno("fork");
870 goto done;
871 } else if (pid == 0) {
872 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
873 NULL);
874 /* not reached */
877 if (close(imsg_fds[1]) == -1) {
878 err = got_error_from_errno("close");
879 goto done;
881 imsg_fds[1] = -1;
882 imsg_init(ibuf, imsg_fds[0]);
884 err = send_patch(ibuf, fd);
885 fd = -1;
886 if (err)
887 goto done;
889 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
890 worktree);
891 if (err)
892 goto done;
894 while (!done && err == NULL) {
895 struct got_patch p;
896 struct patch_args pa;
898 pa.progress_cb = progress_cb;
899 pa.progress_arg = progress_arg;
900 pa.head = &p.head;
902 err = recv_patch(ibuf, &done, &p, strip);
903 if (err || done)
904 break;
906 if (reverse)
907 reverse_patch(&p);
909 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
910 &newpath, worktree, repo, fileindex);
911 if (err == NULL)
912 err = apply_patch(&overlapcnt, worktree, repo,
913 fileindex, oldpath, newpath, &p, nop, &pa,
914 cancel_cb, cancel_arg);
915 if (err != NULL) {
916 failed = 1;
917 /* recoverable errors */
918 if (err->code == GOT_ERR_FILE_STATUS ||
919 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
920 err = report_progress(&pa, p.old, p.new,
921 GOT_STATUS_CANNOT_UPDATE, err);
922 else if (err->code == GOT_ERR_HUNK_FAILED)
923 err = report_progress(&pa, p.old, p.new,
924 GOT_STATUS_CANNOT_UPDATE, NULL);
926 if (overlapcnt != 0)
927 failed = 1;
929 free(oldpath);
930 free(newpath);
931 patch_free(&p);
933 if (err)
934 break;
937 done:
938 if (fileindex != NULL)
939 complete_err = got_worktree_patch_complete(fileindex,
940 fileindex_path);
941 if (complete_err && err == NULL)
942 err = complete_err;
943 free(fileindex_path);
944 if (fd != -1 && close(fd) == -1 && err == NULL)
945 err = got_error_from_errno("close");
946 if (ibuf != NULL)
947 imsg_clear(ibuf);
948 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
949 err = got_error_from_errno("close");
950 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
951 err = got_error_from_errno("close");
952 if (err == NULL && failed)
953 err = got_error(GOT_ERR_PATCH_FAILED);
954 return err;