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 <errno.h>
30 #include <limits.h>
31 #include <sha1.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <imsg.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_path.h"
42 #include "got_reference.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_repository.h"
46 #include "got_opentemp.h"
47 #include "got_patch.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 offset;
61 int old_nonl;
62 int new_nonl;
63 int old_from;
64 int old_lines;
65 int new_from;
66 int new_lines;
67 size_t len;
68 size_t cap;
69 char **lines;
70 };
72 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
73 struct got_patch {
74 char *old;
75 char *new;
76 char cid[41];
77 char blob[41];
78 struct got_patch_hunk_head head;
79 };
81 struct patch_args {
82 got_patch_progress_cb progress_cb;
83 void *progress_arg;
84 struct got_patch_hunk_head *head;
85 };
87 static const struct got_error *
88 send_patch(struct imsgbuf *ibuf, int fd)
89 {
90 const struct got_error *err = NULL;
92 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
93 NULL, 0) == -1) {
94 err = got_error_from_errno(
95 "imsg_compose GOT_IMSG_PATCH_FILE");
96 close(fd);
97 return err;
98 }
100 if (imsg_flush(ibuf) == -1) {
101 err = got_error_from_errno("imsg_flush");
102 imsg_clear(ibuf);
105 return err;
108 static void
109 patch_free(struct got_patch *p)
111 struct got_patch_hunk *h;
112 size_t i;
114 while (!STAILQ_EMPTY(&p->head)) {
115 h = STAILQ_FIRST(&p->head);
116 STAILQ_REMOVE_HEAD(&p->head, entries);
118 for (i = 0; i < h->len; ++i)
119 free(h->lines[i]);
120 free(h->lines);
121 free(h);
124 free(p->new);
125 free(p->old);
127 memset(p, 0, sizeof(*p));
128 STAILQ_INIT(&p->head);
131 static const struct got_error *
132 pushline(struct got_patch_hunk *h, const char *line)
134 void *t;
135 size_t newcap;
137 if (h->len == h->cap) {
138 if ((newcap = h->cap * 1.5) == 0)
139 newcap = 16;
140 t = recallocarray(h->lines, h->cap, newcap,
141 sizeof(h->lines[0]));
142 if (t == NULL)
143 return got_error_from_errno("recallocarray");
144 h->lines = t;
145 h->cap = newcap;
148 if ((t = strdup(line)) == NULL)
149 return got_error_from_errno("strdup");
151 h->lines[h->len++] = t;
152 return NULL;
155 static const struct got_error *
156 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
158 const struct got_error *err = NULL;
159 struct imsg imsg;
160 struct got_imsg_patch_hunk hdr;
161 struct got_imsg_patch patch;
162 struct got_patch_hunk *h = NULL;
163 size_t datalen;
164 int lastmode = -1;
166 memset(p, 0, sizeof(*p));
167 STAILQ_INIT(&p->head);
169 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
170 if (err)
171 return err;
172 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
173 *done = 1;
174 goto done;
176 if (imsg.hdr.type != GOT_IMSG_PATCH) {
177 err = got_error(GOT_ERR_PRIVSEP_MSG);
178 goto done;
180 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
181 if (datalen != sizeof(patch)) {
182 err = got_error(GOT_ERR_PRIVSEP_LEN);
183 goto done;
185 memcpy(&patch, imsg.data, sizeof(patch));
187 if (patch.old[sizeof(patch.old)-1] != '\0' ||
188 patch.new[sizeof(patch.new)-1] != '\0' ||
189 patch.cid[sizeof(patch.cid)-1] != '\0' ||
190 patch.blob[sizeof(patch.blob)-1] != '\0') {
191 err = got_error(GOT_ERR_PRIVSEP_LEN);
192 goto done;
195 if (*patch.cid != '\0')
196 strlcpy(p->cid, patch.cid, sizeof(p->cid));
198 if (*patch.blob != '\0')
199 strlcpy(p->blob, patch.blob, sizeof(p->blob));
201 /* automatically set strip=1 for git-style diffs */
202 if (strip == -1 && patch.git &&
203 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
204 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
205 strip = 1;
207 /* prefer the new name if not /dev/null for not git-style diffs */
208 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
209 err = got_path_strip(&p->old, patch.new, strip);
210 if (err)
211 goto done;
212 } else if (*patch.old != '\0') {
213 err = got_path_strip(&p->old, patch.old, strip);
214 if (err)
215 goto done;
218 if (*patch.new != '\0') {
219 err = got_path_strip(&p->new, patch.new, strip);
220 if (err)
221 goto done;
224 if (p->old == NULL && p->new == NULL) {
225 err = got_error(GOT_ERR_PATCH_MALFORMED);
226 goto done;
229 imsg_free(&imsg);
231 for (;;) {
232 char *t;
234 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
235 if (err) {
236 patch_free(p);
237 return err;
240 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
241 switch (imsg.hdr.type) {
242 case GOT_IMSG_PATCH_DONE:
243 if (h != NULL && h->len == 0)
244 err = got_error(GOT_ERR_PATCH_MALFORMED);
245 goto done;
246 case GOT_IMSG_PATCH_HUNK:
247 if (h != NULL &&
248 (h->len == 0 || h->old_nonl || h->new_nonl)) {
249 err = got_error(GOT_ERR_PATCH_MALFORMED);
250 goto done;
252 lastmode = -1;
253 if (datalen != sizeof(hdr)) {
254 err = got_error(GOT_ERR_PRIVSEP_LEN);
255 goto done;
257 memcpy(&hdr, imsg.data, sizeof(hdr));
258 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
259 err = got_error(GOT_ERR_PRIVSEP_LEN);
260 goto done;
262 if ((h = calloc(1, sizeof(*h))) == NULL) {
263 err = got_error_from_errno("calloc");
264 goto done;
266 h->old_from = hdr.oldfrom;
267 h->old_lines = hdr.oldlines;
268 h->new_from = hdr.newfrom;
269 h->new_lines = hdr.newlines;
270 STAILQ_INSERT_TAIL(&p->head, h, entries);
271 break;
272 case GOT_IMSG_PATCH_LINE:
273 if (h == NULL) {
274 err = got_error(GOT_ERR_PRIVSEP_MSG);
275 goto done;
277 t = imsg.data;
278 /* at least one char */
279 if (datalen < 2 || t[datalen-1] != '\0') {
280 err = got_error(GOT_ERR_PRIVSEP_MSG);
281 goto done;
283 if (*t != ' ' && *t != '-' && *t != '+' &&
284 *t != '\\') {
285 err = got_error(GOT_ERR_PRIVSEP_MSG);
286 goto done;
289 if (*t != '\\')
290 err = pushline(h, t);
291 else if (lastmode == '-')
292 h->old_nonl = 1;
293 else if (lastmode == '+')
294 h->new_nonl = 1;
295 else
296 err = got_error(GOT_ERR_PATCH_MALFORMED);
298 if (err)
299 goto done;
301 lastmode = *t;
302 break;
303 default:
304 err = got_error(GOT_ERR_PRIVSEP_MSG);
305 goto done;
308 imsg_free(&imsg);
311 done:
312 if (err)
313 patch_free(p);
315 imsg_free(&imsg);
316 return err;
319 /*
320 * Copy data from orig starting at copypos until pos into tmp.
321 * If pos is -1, copy until EOF.
322 */
323 static const struct got_error *
324 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
326 char buf[BUFSIZ];
327 size_t len, r, w;
329 if (fseeko(orig, copypos, SEEK_SET) == -1)
330 return got_error_from_errno("fseeko");
332 while (pos == -1 || copypos < pos) {
333 len = sizeof(buf);
334 if (pos > 0)
335 len = MIN(len, (size_t)pos - copypos);
336 r = fread(buf, 1, len, orig);
337 if (r != len && ferror(orig))
338 return got_error_from_errno("fread");
339 w = fwrite(buf, 1, r, tmp);
340 if (w != r)
341 return got_error_from_errno("fwrite");
342 copypos += len;
343 if (r != len && feof(orig)) {
344 if (pos == -1)
345 return NULL;
346 return got_error(GOT_ERR_HUNK_FAILED);
349 return NULL;
352 static const struct got_error *
353 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
355 const struct got_error *err = NULL;
356 char *line = NULL;
357 char mode = *h->lines[0];
358 size_t linesize = 0;
359 ssize_t linelen;
360 off_t match = -1;
361 int match_lineno = -1;
363 for (;;) {
364 linelen = getline(&line, &linesize, orig);
365 if (linelen == -1) {
366 if (ferror(orig))
367 err = got_error_from_errno("getline");
368 else if (match == -1)
369 err = got_error(GOT_ERR_HUNK_FAILED);
370 break;
372 if (line[linelen - 1] == '\n')
373 line[linelen - 1] = '\0';
374 (*lineno)++;
376 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
377 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
378 (mode == '+' && *lineno == h->old_from)) {
379 match = ftello(orig);
380 if (match == -1) {
381 err = got_error_from_errno("ftello");
382 break;
384 match -= linelen;
385 match_lineno = (*lineno)-1;
388 if (*lineno >= h->old_from && match != -1)
389 break;
392 if (err == NULL) {
393 *pos = match;
394 *lineno = match_lineno;
395 if (fseeko(orig, match, SEEK_SET) == -1)
396 err = got_error_from_errno("fseeko");
399 free(line);
400 return err;
403 static const struct got_error *
404 test_hunk(FILE *orig, struct got_patch_hunk *h)
406 const struct got_error *err = NULL;
407 char *line = NULL;
408 size_t linesize = 0, i = 0;
409 ssize_t linelen;
411 for (i = 0; i < h->len; ++i) {
412 switch (*h->lines[i]) {
413 case '+':
414 continue;
415 case ' ':
416 case '-':
417 linelen = getline(&line, &linesize, orig);
418 if (linelen == -1) {
419 if (ferror(orig))
420 err = got_error_from_errno("getline");
421 else
422 err = got_error(
423 GOT_ERR_HUNK_FAILED);
424 goto done;
426 if (line[linelen - 1] == '\n')
427 line[linelen - 1] = '\0';
428 if (strcmp(h->lines[i] + 1, line)) {
429 err = got_error(GOT_ERR_HUNK_FAILED);
430 goto done;
432 break;
436 done:
437 free(line);
438 return err;
441 static const struct got_error *
442 apply_hunk(FILE *tmp, struct got_patch_hunk *h, int *lineno)
444 size_t i, new = 0;
446 for (i = 0; i < h->len; ++i) {
447 switch (*h->lines[i]) {
448 case ' ':
449 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
450 return got_error_from_errno("fprintf");
451 /* fallthrough */
452 case '-':
453 (*lineno)++;
454 break;
455 case '+':
456 new++;
457 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
458 return got_error_from_errno("fprintf");
459 if (new != h->new_lines || !h->new_nonl) {
460 if (fprintf(tmp, "\n") < 0)
461 return got_error_from_errno(
462 "fprintf");
464 break;
467 return NULL;
470 static const struct got_error *
471 patch_file(struct got_patch *p, FILE *orig, FILE *tmp, mode_t *mode)
473 const struct got_error *err = NULL;
474 struct got_patch_hunk *h;
475 struct stat sb;
476 int lineno = 0;
477 off_t copypos, pos;
478 char *line = NULL;
479 size_t linesize = 0;
480 ssize_t linelen;
482 if (p->old == NULL) { /* create */
483 h = STAILQ_FIRST(&p->head);
484 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
485 return got_error(GOT_ERR_PATCH_MALFORMED);
486 return apply_hunk(tmp, h, &lineno);
489 if (fstat(fileno(orig), &sb) == -1)
490 return got_error_from_errno("fstat");
491 *mode = sb.st_mode;
493 copypos = 0;
494 STAILQ_FOREACH(h, &p->head, entries) {
495 tryagain:
496 err = locate_hunk(orig, h, &pos, &lineno);
497 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
498 h->err = err;
499 if (err != NULL)
500 return err;
501 err = copy(tmp, orig, copypos, pos);
502 if (err != NULL)
503 return err;
504 copypos = pos;
506 err = test_hunk(orig, h);
507 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
508 /*
509 * try to apply the hunk again starting the search
510 * after the previous partial match.
511 */
512 if (fseeko(orig, pos, SEEK_SET) == -1)
513 return got_error_from_errno("fseeko");
514 linelen = getline(&line, &linesize, orig);
515 if (linelen == -1)
516 return got_error_from_errno("getline");
517 lineno++;
518 goto tryagain;
520 if (err != NULL)
521 return err;
523 if (lineno + 1 != h->old_from)
524 h->offset = lineno + 1 - h->old_from;
526 err = apply_hunk(tmp, h, &lineno);
527 if (err != NULL)
528 return err;
530 copypos = ftello(orig);
531 if (copypos == -1)
532 return got_error_from_errno("ftello");
535 if (p->new == NULL && sb.st_size != copypos) {
536 h = STAILQ_FIRST(&p->head);
537 h->err = got_error(GOT_ERR_HUNK_FAILED);
538 err = h->err;
539 } else if (!feof(orig))
540 err = copy(tmp, orig, copypos, -1);
542 return err;
545 static const struct got_error *
546 report_progress(struct patch_args *pa, const char *old, const char *new,
547 unsigned char status, const struct got_error *orig_error)
549 const struct got_error *err;
550 struct got_patch_hunk *h;
552 err = pa->progress_cb(pa->progress_arg, old, new, status,
553 orig_error, 0, 0, 0, 0, 0, NULL);
554 if (err)
555 return err;
557 STAILQ_FOREACH(h, pa->head, entries) {
558 if (h->offset == 0 && h->err == NULL)
559 continue;
561 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
562 h->old_from, h->old_lines, h->new_from, h->new_lines,
563 h->offset, h->err);
564 if (err)
565 return err;
568 return NULL;
571 static const struct got_error *
572 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
573 const char *path)
575 return report_progress(arg, path, NULL, status, NULL);
578 static const struct got_error *
579 patch_add(void *arg, unsigned char status, const char *path)
581 return report_progress(arg, NULL, path, status, NULL);
584 static const struct got_error *
585 open_blob(char **path, FILE **fp, const char *blobid,
586 struct got_repository *repo)
588 const struct got_error *err = NULL;
589 struct got_blob_object *blob = NULL;
590 struct got_object_id id, *idptr, *matched_id = NULL;
591 int fd = -1;
593 *fp = NULL;
594 *path = NULL;
596 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
597 err = got_repo_match_object_id(&matched_id, NULL, blobid,
598 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
599 repo);
600 if (err)
601 return err;
602 idptr = matched_id;
603 } else {
604 if (!got_parse_sha1_digest(id.sha1, blobid))
605 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
606 idptr = &id;
609 fd = got_opentempfd();
610 if (fd == -1) {
611 err = got_error_from_errno("got_opentempfd");
612 goto done;
615 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
616 if (err)
617 goto done;
619 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
620 if (err)
621 goto done;
623 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
624 if (err)
625 goto done;
627 done:
628 if (fd != -1 && close(fd) == -1 && err == NULL)
629 err = got_error_from_errno("close");
630 if (blob)
631 got_object_blob_close(blob);
632 if (matched_id != NULL)
633 free(matched_id);
634 if (err) {
635 if (*fp != NULL)
636 fclose(*fp);
637 if (*path != NULL)
638 unlink(*path);
639 free(*path);
640 *fp = NULL;
641 *path = NULL;
643 return err;
646 static const struct got_error *
647 apply_patch(int *overlapcnt, struct got_worktree *worktree,
648 struct got_repository *repo, struct got_fileindex *fileindex,
649 const char *old, const char *new, struct got_patch *p, int nop,
650 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
652 const struct got_error *err = NULL;
653 int do_merge = 0, file_renamed = 0;
654 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
655 char *oldpath = NULL, *newpath = NULL;
656 char *tmppath = NULL, *template = NULL, *parent = NULL;
657 char *apath = NULL, *mergepath = NULL;
658 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
659 int outfd;
660 const char *outpath;
661 mode_t mode = GOT_DEFAULT_FILE_MODE;
663 *overlapcnt = 0;
665 /* don't run the diff3 merge on creations/deletions */
666 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
667 err = open_blob(&apath, &afile, p->blob, repo);
668 /*
669 * ignore failures to open this blob, we might have
670 * parsed gibberish.
671 */
672 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
673 err->code != GOT_ERR_NO_OBJ)
674 return err;
675 else if (err == NULL)
676 do_merge = 1;
677 err = NULL;
680 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
681 old) == -1) {
682 err = got_error_from_errno("asprintf");
683 goto done;
686 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
687 new) == -1) {
688 err = got_error_from_errno("asprintf");
689 goto done;
692 file_renamed = strcmp(oldpath, newpath);
694 if (asprintf(&template, "%s/got-patch",
695 got_worktree_get_root_path(worktree)) == -1) {
696 err = got_error_from_errno(template);
697 goto done;
700 if (p->old != NULL && (oldfile = fopen(oldpath, "r")) == NULL) {
701 err = got_error_from_errno2("open", oldpath);
702 goto done;
705 err = got_opentemp_named(&tmppath, &tmpfile, template);
706 if (err)
707 goto done;
708 outpath = tmppath;
709 outfd = fileno(tmpfile);
710 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile, &mode);
711 if (err)
712 goto done;
714 if (do_merge) {
715 const char *type, *id;
717 if (fseeko(afile, 0, SEEK_SET) == -1 ||
718 fseeko(oldfile, 0, SEEK_SET) == -1 ||
719 fseeko(tmpfile, 0, SEEK_SET) == -1) {
720 err = got_error_from_errno("fseeko");
721 goto done;
724 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
725 err = got_error_from_errno("asprintf");
726 oldlabel = NULL;
727 goto done;
730 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
731 err = got_error_from_errno("asprintf");
732 newlabel = NULL;
733 goto done;
736 if (*p->cid != '\0') {
737 type = "commit";
738 id = p->cid;
739 } else {
740 type = "blob";
741 id = p->blob;
744 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
745 err = got_error_from_errno("asprintf");
746 anclabel = NULL;
747 goto done;
750 err = got_opentemp_named(&mergepath, &mergefile, template);
751 if (err)
752 goto done;
753 outpath = mergepath;
754 outfd = fileno(mergefile);
756 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
757 oldfile, oldlabel, anclabel, newlabel,
758 GOT_DIFF_ALGORITHM_PATIENCE);
759 if (err)
760 goto done;
763 if (nop)
764 goto done;
766 if (p->old != NULL && p->new == NULL) {
767 err = got_worktree_patch_schedule_rm(old, repo, worktree,
768 fileindex, patch_delete, pa);
769 goto done;
772 if (fchmod(outfd, mode) == -1) {
773 err = got_error_from_errno2("chmod", tmppath);
774 goto done;
777 if (rename(outpath, newpath) == -1) {
778 if (errno != ENOENT) {
779 err = got_error_from_errno3("rename", outpath,
780 newpath);
781 goto done;
784 err = got_path_dirname(&parent, newpath);
785 if (err != NULL)
786 goto done;
787 err = got_path_mkdir(parent);
788 if (err != NULL)
789 goto done;
790 if (rename(outpath, newpath) == -1) {
791 err = got_error_from_errno3("rename", outpath,
792 newpath);
793 goto done;
797 if (file_renamed) {
798 err = got_worktree_patch_schedule_rm(old, repo, worktree,
799 fileindex, patch_delete, pa);
800 if (err == NULL)
801 err = got_worktree_patch_schedule_add(new, repo,
802 worktree, fileindex, patch_add,
803 pa);
804 if (err)
805 unlink(newpath);
806 } else if (p->old == NULL) {
807 err = got_worktree_patch_schedule_add(new, repo, worktree,
808 fileindex, patch_add, pa);
809 if (err)
810 unlink(newpath);
811 } else if (*overlapcnt != 0)
812 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
813 else if (do_merge)
814 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
815 else
816 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
818 done:
819 free(parent);
820 free(template);
822 if (tmppath != NULL)
823 unlink(tmppath);
824 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
825 err = got_error_from_errno("fclose");
826 free(tmppath);
828 free(oldpath);
829 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
830 err = got_error_from_errno("fclose");
832 if (apath != NULL)
833 unlink(apath);
834 if (afile != NULL && fclose(afile) == EOF && err == NULL)
835 err = got_error_from_errno("fclose");
836 free(apath);
838 if (mergepath != NULL)
839 unlink(mergepath);
840 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
841 err = got_error_from_errno("fclose");
842 free(mergepath);
844 free(newpath);
845 free(oldlabel);
846 free(newlabel);
847 free(anclabel);
848 return err;
851 static void
852 reverse_patch(struct got_patch *p)
854 struct got_patch_hunk *h;
855 size_t i;
856 int tmp;
858 STAILQ_FOREACH(h, &p->head, entries) {
859 tmp = h->old_from;
860 h->old_from = h->new_from;
861 h->new_from = tmp;
863 tmp = h->old_lines;
864 h->old_lines = h->new_lines;
865 h->new_lines = tmp;
867 tmp = h->old_nonl;
868 h->old_nonl = h->new_nonl;
869 h->new_nonl = tmp;
871 for (i = 0; i < h->len; ++i) {
872 if (*h->lines[i] == '+')
873 *h->lines[i] = '-';
874 else if (*h->lines[i] == '-')
875 *h->lines[i] = '+';
880 const struct got_error *
881 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
882 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
883 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
885 const struct got_error *err = NULL, *complete_err = NULL;
886 struct got_fileindex *fileindex = NULL;
887 char *fileindex_path = NULL;
888 char *oldpath, *newpath;
889 struct imsgbuf *ibuf;
890 int imsg_fds[2] = {-1, -1};
891 int overlapcnt, done = 0, failed = 0;
892 pid_t pid;
894 ibuf = calloc(1, sizeof(*ibuf));
895 if (ibuf == NULL) {
896 err = got_error_from_errno("calloc");
897 goto done;
900 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
901 err = got_error_from_errno("socketpair");
902 goto done;
905 pid = fork();
906 if (pid == -1) {
907 err = got_error_from_errno("fork");
908 goto done;
909 } else if (pid == 0) {
910 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
911 NULL);
912 /* not reached */
915 if (close(imsg_fds[1]) == -1) {
916 err = got_error_from_errno("close");
917 goto done;
919 imsg_fds[1] = -1;
920 imsg_init(ibuf, imsg_fds[0]);
922 err = send_patch(ibuf, fd);
923 fd = -1;
924 if (err)
925 goto done;
927 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
928 worktree);
929 if (err)
930 goto done;
932 while (!done && err == NULL) {
933 struct got_patch p;
934 struct patch_args pa;
936 pa.progress_cb = progress_cb;
937 pa.progress_arg = progress_arg;
938 pa.head = &p.head;
940 err = recv_patch(ibuf, &done, &p, strip);
941 if (err || done)
942 break;
944 if (reverse)
945 reverse_patch(&p);
947 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
948 &newpath, worktree, repo, fileindex);
949 if (err == NULL)
950 err = apply_patch(&overlapcnt, worktree, repo,
951 fileindex, oldpath, newpath, &p, nop, &pa,
952 cancel_cb, cancel_arg);
953 if (err != NULL) {
954 failed = 1;
955 /* recoverable errors */
956 if (err->code == GOT_ERR_FILE_STATUS ||
957 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
958 err = report_progress(&pa, p.old, p.new,
959 GOT_STATUS_CANNOT_UPDATE, err);
960 else if (err->code == GOT_ERR_HUNK_FAILED)
961 err = report_progress(&pa, p.old, p.new,
962 GOT_STATUS_CANNOT_UPDATE, NULL);
964 if (overlapcnt != 0)
965 failed = 1;
967 free(oldpath);
968 free(newpath);
969 patch_free(&p);
971 if (err)
972 break;
975 done:
976 if (fileindex != NULL)
977 complete_err = got_worktree_patch_complete(fileindex,
978 fileindex_path);
979 if (complete_err && err == NULL)
980 err = complete_err;
981 free(fileindex_path);
982 if (fd != -1 && close(fd) == -1 && err == NULL)
983 err = got_error_from_errno("close");
984 if (ibuf != NULL)
985 imsg_clear(ibuf);
986 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
987 err = got_error_from_errno("close");
988 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
989 err = got_error_from_errno("close");
990 if (err == NULL && failed)
991 err = got_error(GOT_ERR_PATCH_FAILED);
992 return err;