Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 *
16 * Apply patches.
17 *
18 * Things that we may want to support:
19 * + support indented patches?
20 * + support other kinds of patches?
21 */
23 #include <sys/types.h>
24 #include <sys/queue.h>
25 #include <sys/socket.h>
26 #include <sys/stat.h>
27 #include <sys/uio.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <sha1.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <imsg.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_reference.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_repository.h"
47 #include "got_opentemp.h"
48 #include "got_patch.h"
49 #include "got_diff.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_diff.h"
53 #include "got_lib_object.h"
54 #include "got_lib_privsep.h"
55 #include "got_lib_sha1.h"
57 #define MIN(a, b) ((a) < (b) ? (a) : (b))
59 struct got_patch_hunk {
60 STAILQ_ENTRY(got_patch_hunk) entries;
61 const struct got_error *err;
62 int ws_mangled;
63 int offset;
64 int old_nonl;
65 int new_nonl;
66 int old_from;
67 int old_lines;
68 int new_from;
69 int new_lines;
70 size_t len;
71 size_t cap;
72 char **lines;
73 };
75 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
76 struct got_patch {
77 char *old;
78 char *new;
79 char cid[41];
80 char blob[41];
81 struct got_patch_hunk_head head;
82 };
84 struct patch_args {
85 got_patch_progress_cb progress_cb;
86 void *progress_arg;
87 struct got_patch_hunk_head *head;
88 };
90 static const struct got_error *
91 send_patch(struct imsgbuf *ibuf, int fd)
92 {
93 const struct got_error *err = NULL;
95 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
96 NULL, 0) == -1) {
97 err = got_error_from_errno(
98 "imsg_compose GOT_IMSG_PATCH_FILE");
99 close(fd);
100 return err;
103 if (imsg_flush(ibuf) == -1) {
104 err = got_error_from_errno("imsg_flush");
105 imsg_clear(ibuf);
108 return err;
111 static void
112 patch_free(struct got_patch *p)
114 struct got_patch_hunk *h;
115 size_t i;
117 while (!STAILQ_EMPTY(&p->head)) {
118 h = STAILQ_FIRST(&p->head);
119 STAILQ_REMOVE_HEAD(&p->head, entries);
121 for (i = 0; i < h->len; ++i)
122 free(h->lines[i]);
123 free(h->lines);
124 free(h);
127 free(p->new);
128 free(p->old);
130 memset(p, 0, sizeof(*p));
131 STAILQ_INIT(&p->head);
134 static const struct got_error *
135 pushline(struct got_patch_hunk *h, const char *line)
137 void *t;
138 size_t newcap;
140 if (h->len == h->cap) {
141 if ((newcap = h->cap * 1.5) == 0)
142 newcap = 16;
143 t = recallocarray(h->lines, h->cap, newcap,
144 sizeof(h->lines[0]));
145 if (t == NULL)
146 return got_error_from_errno("recallocarray");
147 h->lines = t;
148 h->cap = newcap;
151 if ((t = strdup(line)) == NULL)
152 return got_error_from_errno("strdup");
154 h->lines[h->len++] = t;
155 return NULL;
158 static const struct got_error *
159 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
161 const struct got_error *err = NULL;
162 struct imsg imsg;
163 struct got_imsg_patch_hunk hdr;
164 struct got_imsg_patch patch;
165 struct got_patch_hunk *h = NULL;
166 size_t datalen;
167 int lastmode = -1;
169 memset(p, 0, sizeof(*p));
170 STAILQ_INIT(&p->head);
172 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
173 if (err)
174 return err;
175 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
176 *done = 1;
177 goto done;
179 if (imsg.hdr.type != GOT_IMSG_PATCH) {
180 err = got_error(GOT_ERR_PRIVSEP_MSG);
181 goto done;
183 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
184 if (datalen != sizeof(patch)) {
185 err = got_error(GOT_ERR_PRIVSEP_LEN);
186 goto done;
188 memcpy(&patch, imsg.data, sizeof(patch));
190 if (patch.old[sizeof(patch.old)-1] != '\0' ||
191 patch.new[sizeof(patch.new)-1] != '\0' ||
192 patch.cid[sizeof(patch.cid)-1] != '\0' ||
193 patch.blob[sizeof(patch.blob)-1] != '\0') {
194 err = got_error(GOT_ERR_PRIVSEP_LEN);
195 goto done;
198 if (*patch.cid != '\0')
199 strlcpy(p->cid, patch.cid, sizeof(p->cid));
201 if (*patch.blob != '\0')
202 strlcpy(p->blob, patch.blob, sizeof(p->blob));
204 /* automatically set strip=1 for git-style diffs */
205 if (strip == -1 && patch.git &&
206 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
207 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
208 strip = 1;
210 /* prefer the new name if not /dev/null for not git-style diffs */
211 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
212 err = got_path_strip(&p->old, patch.new, strip);
213 if (err)
214 goto done;
215 } else if (*patch.old != '\0') {
216 err = got_path_strip(&p->old, patch.old, strip);
217 if (err)
218 goto done;
221 if (*patch.new != '\0') {
222 err = got_path_strip(&p->new, patch.new, strip);
223 if (err)
224 goto done;
227 if (p->old == NULL && p->new == NULL) {
228 err = got_error(GOT_ERR_PATCH_MALFORMED);
229 goto done;
232 imsg_free(&imsg);
234 for (;;) {
235 char *t;
237 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
238 if (err) {
239 patch_free(p);
240 return err;
243 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
244 switch (imsg.hdr.type) {
245 case GOT_IMSG_PATCH_DONE:
246 if (h != NULL && h->len == 0)
247 err = got_error(GOT_ERR_PATCH_MALFORMED);
248 goto done;
249 case GOT_IMSG_PATCH_HUNK:
250 if (h != NULL &&
251 (h->len == 0 || h->old_nonl || h->new_nonl)) {
252 err = got_error(GOT_ERR_PATCH_MALFORMED);
253 goto done;
255 lastmode = -1;
256 if (datalen != sizeof(hdr)) {
257 err = got_error(GOT_ERR_PRIVSEP_LEN);
258 goto done;
260 memcpy(&hdr, imsg.data, sizeof(hdr));
261 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
262 err = got_error(GOT_ERR_PRIVSEP_LEN);
263 goto done;
265 if ((h = calloc(1, sizeof(*h))) == NULL) {
266 err = got_error_from_errno("calloc");
267 goto done;
269 h->old_from = hdr.oldfrom;
270 h->old_lines = hdr.oldlines;
271 h->new_from = hdr.newfrom;
272 h->new_lines = hdr.newlines;
273 STAILQ_INSERT_TAIL(&p->head, h, entries);
274 break;
275 case GOT_IMSG_PATCH_LINE:
276 if (h == NULL) {
277 err = got_error(GOT_ERR_PRIVSEP_MSG);
278 goto done;
280 t = imsg.data;
281 /* at least one char */
282 if (datalen < 2 || t[datalen-1] != '\0') {
283 err = got_error(GOT_ERR_PRIVSEP_MSG);
284 goto done;
286 if (*t != ' ' && *t != '-' && *t != '+' &&
287 *t != '\\') {
288 err = got_error(GOT_ERR_PRIVSEP_MSG);
289 goto done;
292 if (*t != '\\')
293 err = pushline(h, t);
294 else if (lastmode == '-')
295 h->old_nonl = 1;
296 else if (lastmode == '+')
297 h->new_nonl = 1;
298 else
299 err = got_error(GOT_ERR_PATCH_MALFORMED);
301 if (err)
302 goto done;
304 lastmode = *t;
305 break;
306 default:
307 err = got_error(GOT_ERR_PRIVSEP_MSG);
308 goto done;
311 imsg_free(&imsg);
314 done:
315 if (err)
316 patch_free(p);
318 imsg_free(&imsg);
319 return err;
322 /*
323 * Copy data from orig starting at copypos until pos into tmp.
324 * If pos is -1, copy until EOF.
325 */
326 static const struct got_error *
327 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
329 char buf[BUFSIZ];
330 size_t len, r, w;
332 if (fseeko(orig, copypos, SEEK_SET) == -1)
333 return got_error_from_errno("fseeko");
335 while (pos == -1 || copypos < pos) {
336 len = sizeof(buf);
337 if (pos > 0)
338 len = MIN(len, (size_t)pos - copypos);
339 r = fread(buf, 1, len, orig);
340 if (r != len && ferror(orig))
341 return got_error_from_errno("fread");
342 w = fwrite(buf, 1, r, tmp);
343 if (w != r)
344 return got_error_from_errno("fwrite");
345 copypos += len;
346 if (r != len && feof(orig)) {
347 if (pos == -1)
348 return NULL;
349 return got_error(GOT_ERR_HUNK_FAILED);
352 return NULL;
355 static const struct got_error *
356 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
358 const struct got_error *err = NULL;
359 char *line = NULL;
360 char mode = *h->lines[0];
361 size_t linesize = 0;
362 ssize_t linelen;
363 off_t match = -1;
364 int match_lineno = -1;
366 for (;;) {
367 linelen = getline(&line, &linesize, orig);
368 if (linelen == -1) {
369 if (ferror(orig))
370 err = got_error_from_errno("getline");
371 else if (match == -1)
372 err = got_error(GOT_ERR_HUNK_FAILED);
373 break;
375 if (line[linelen - 1] == '\n')
376 line[linelen - 1] = '\0';
377 (*lineno)++;
379 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
380 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
381 (mode == '+' && *lineno == h->old_from)) {
382 match = ftello(orig);
383 if (match == -1) {
384 err = got_error_from_errno("ftello");
385 break;
387 match -= linelen;
388 match_lineno = (*lineno)-1;
391 if (*lineno >= h->old_from && match != -1)
392 break;
395 if (err == NULL) {
396 *pos = match;
397 *lineno = match_lineno;
398 if (fseeko(orig, match, SEEK_SET) == -1)
399 err = got_error_from_errno("fseeko");
402 free(line);
403 return err;
406 static int
407 linecmp(const char *a, const char *b, int *mangled)
409 int c;
411 *mangled = 0;
412 c = strcmp(a, b);
413 if (c == 0)
414 return c;
416 *mangled = 1;
417 for (;;) {
418 while (*a == '\t' || *a == ' ' || *a == '\f')
419 a++;
420 while (*b == '\t' || *b == ' ' || *b == '\f')
421 b++;
422 if (*a == '\0' || *a != *b)
423 break;
424 a++, b++;
427 return *a - *b;
430 static const struct got_error *
431 test_hunk(FILE *orig, struct got_patch_hunk *h)
433 const struct got_error *err = NULL;
434 char *line = NULL;
435 size_t linesize = 0, i = 0;
436 ssize_t linelen;
437 int mangled;
439 for (i = 0; i < h->len; ++i) {
440 switch (*h->lines[i]) {
441 case '+':
442 continue;
443 case ' ':
444 case '-':
445 linelen = getline(&line, &linesize, orig);
446 if (linelen == -1) {
447 if (ferror(orig))
448 err = got_error_from_errno("getline");
449 else
450 err = got_error(
451 GOT_ERR_HUNK_FAILED);
452 goto done;
454 if (line[linelen - 1] == '\n')
455 line[linelen - 1] = '\0';
456 if (linecmp(h->lines[i] + 1, line, &mangled)) {
457 err = got_error(GOT_ERR_HUNK_FAILED);
458 goto done;
460 if (mangled)
461 h->ws_mangled = 1;
462 break;
466 done:
467 free(line);
468 return err;
471 static const struct got_error *
472 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
473 off_t from)
475 const struct got_error *err = NULL;
476 const char *t;
477 size_t linesize = 0, i, new = 0;
478 char *line = NULL;
479 char mode;
480 ssize_t linelen;
482 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
483 return got_error_from_errno("fseeko");
485 for (i = 0; i < h->len; ++i) {
486 switch (mode = *h->lines[i]) {
487 case '-':
488 case ' ':
489 (*lineno)++;
490 if (orig != NULL) {
491 linelen = getline(&line, &linesize, orig);
492 if (linelen == -1) {
493 err = got_error_from_errno("getline");
494 goto done;
496 if (line[linelen - 1] == '\n')
497 line[linelen - 1] = '\0';
498 t = line;
499 } else
500 t = h->lines[i] + 1;
501 if (mode == '-')
502 continue;
503 if (fprintf(tmp, "%s\n", t) < 0) {
504 err = got_error_from_errno("fprintf");
505 goto done;
507 break;
508 case '+':
509 new++;
510 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
511 err = got_error_from_errno("fprintf");
512 goto done;
514 if (new != h->new_lines || !h->new_nonl) {
515 if (fprintf(tmp, "\n") < 0) {
516 err = got_error_from_errno("fprintf");
517 goto done;
520 break;
524 done:
525 free(line);
526 return err;
529 static const struct got_error *
530 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
532 const struct got_error *err = NULL;
533 struct got_patch_hunk *h;
534 struct stat sb;
535 int lineno = 0;
536 off_t copypos, pos;
537 char *line = NULL;
538 size_t linesize = 0;
539 ssize_t linelen;
541 if (p->old == NULL) { /* create */
542 h = STAILQ_FIRST(&p->head);
543 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
544 return got_error(GOT_ERR_PATCH_MALFORMED);
545 return apply_hunk(orig, tmp, h, &lineno, 0);
548 if (fstat(fileno(orig), &sb) == -1)
549 return got_error_from_errno("fstat");
551 copypos = 0;
552 STAILQ_FOREACH(h, &p->head, entries) {
553 tryagain:
554 err = locate_hunk(orig, h, &pos, &lineno);
555 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
556 h->err = err;
557 if (err != NULL)
558 return err;
559 err = copy(tmp, orig, copypos, pos);
560 if (err != NULL)
561 return err;
562 copypos = pos;
564 err = test_hunk(orig, h);
565 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
566 /*
567 * try to apply the hunk again starting the search
568 * after the previous partial match.
569 */
570 if (fseeko(orig, pos, SEEK_SET) == -1)
571 return got_error_from_errno("fseeko");
572 linelen = getline(&line, &linesize, orig);
573 if (linelen == -1)
574 return got_error_from_errno("getline");
575 lineno++;
576 goto tryagain;
578 if (err != NULL)
579 return err;
581 if (lineno + 1 != h->old_from)
582 h->offset = lineno + 1 - h->old_from;
584 err = apply_hunk(orig, tmp, h, &lineno, pos);
585 if (err != NULL)
586 return err;
588 copypos = ftello(orig);
589 if (copypos == -1)
590 return got_error_from_errno("ftello");
593 if (p->new == NULL && sb.st_size != copypos) {
594 h = STAILQ_FIRST(&p->head);
595 h->err = got_error(GOT_ERR_HUNK_FAILED);
596 err = h->err;
597 } else if (!feof(orig))
598 err = copy(tmp, orig, copypos, -1);
600 return err;
603 static const struct got_error *
604 report_progress(struct patch_args *pa, const char *old, const char *new,
605 unsigned char status, const struct got_error *orig_error)
607 const struct got_error *err;
608 struct got_patch_hunk *h;
610 err = pa->progress_cb(pa->progress_arg, old, new, status,
611 orig_error, 0, 0, 0, 0, 0, 0, NULL);
612 if (err)
613 return err;
615 STAILQ_FOREACH(h, pa->head, entries) {
616 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
617 continue;
619 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
620 h->old_from, h->old_lines, h->new_from, h->new_lines,
621 h->offset, h->ws_mangled, h->err);
622 if (err)
623 return err;
626 return NULL;
629 static const struct got_error *
630 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
631 const char *path)
633 return report_progress(arg, path, NULL, status, NULL);
636 static const struct got_error *
637 patch_add(void *arg, unsigned char status, const char *path)
639 return report_progress(arg, NULL, path, status, NULL);
642 static const struct got_error *
643 open_blob(char **path, FILE **fp, const char *blobid,
644 struct got_repository *repo)
646 const struct got_error *err = NULL;
647 struct got_blob_object *blob = NULL;
648 struct got_object_id id, *idptr, *matched_id = NULL;
649 int fd = -1;
651 *fp = NULL;
652 *path = NULL;
654 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
655 err = got_repo_match_object_id(&matched_id, NULL, blobid,
656 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
657 repo);
658 if (err)
659 return err;
660 idptr = matched_id;
661 } else {
662 if (!got_parse_sha1_digest(id.sha1, blobid))
663 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
664 idptr = &id;
667 fd = got_opentempfd();
668 if (fd == -1) {
669 err = got_error_from_errno("got_opentempfd");
670 goto done;
673 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
674 if (err)
675 goto done;
677 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
678 if (err)
679 goto done;
681 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
682 if (err)
683 goto done;
685 done:
686 if (fd != -1 && close(fd) == -1 && err == NULL)
687 err = got_error_from_errno("close");
688 if (blob)
689 got_object_blob_close(blob);
690 if (matched_id != NULL)
691 free(matched_id);
692 if (err) {
693 if (*fp != NULL)
694 fclose(*fp);
695 if (*path != NULL)
696 unlink(*path);
697 free(*path);
698 *fp = NULL;
699 *path = NULL;
701 return err;
704 static const struct got_error *
705 apply_patch(int *overlapcnt, struct got_worktree *worktree,
706 struct got_repository *repo, struct got_fileindex *fileindex,
707 const char *old, const char *new, struct got_patch *p, int nop,
708 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
710 const struct got_error *err = NULL;
711 struct stat sb;
712 int do_merge = 0, file_renamed = 0;
713 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
714 char *oldpath = NULL, *newpath = NULL;
715 char *tmppath = NULL, *template = NULL, *parent = NULL;
716 char *apath = NULL, *mergepath = NULL;
717 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
718 int outfd;
719 const char *outpath;
720 mode_t mode = GOT_DEFAULT_FILE_MODE;
722 *overlapcnt = 0;
724 /* don't run the diff3 merge on creations/deletions */
725 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
726 err = open_blob(&apath, &afile, p->blob, repo);
727 /*
728 * ignore failures to open this blob, we might have
729 * parsed gibberish.
730 */
731 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
732 err->code != GOT_ERR_NO_OBJ)
733 return err;
734 else if (err == NULL)
735 do_merge = 1;
736 err = NULL;
739 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
740 old) == -1) {
741 err = got_error_from_errno("asprintf");
742 goto done;
745 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
746 new) == -1) {
747 err = got_error_from_errno("asprintf");
748 goto done;
751 file_renamed = strcmp(oldpath, newpath);
753 if (asprintf(&template, "%s/got-patch",
754 got_worktree_get_root_path(worktree)) == -1) {
755 err = got_error_from_errno(template);
756 goto done;
759 if (p->old != NULL) {
760 if ((oldfile = fopen(oldpath, "r")) == NULL) {
761 err = got_error_from_errno2("open", oldpath);
762 goto done;
764 if (fstat(fileno(oldfile), &sb) == -1) {
765 err = got_error_from_errno2("fstat", oldpath);
766 goto done;
768 mode = sb.st_mode;
771 err = got_opentemp_named(&tmppath, &tmpfile, template);
772 if (err)
773 goto done;
774 outpath = tmppath;
775 outfd = fileno(tmpfile);
776 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
777 if (err)
778 goto done;
780 if (do_merge) {
781 const char *type, *id;
783 if (fseeko(afile, 0, SEEK_SET) == -1 ||
784 fseeko(oldfile, 0, SEEK_SET) == -1 ||
785 fseeko(tmpfile, 0, SEEK_SET) == -1) {
786 err = got_error_from_errno("fseeko");
787 goto done;
790 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
791 err = got_error_from_errno("asprintf");
792 oldlabel = NULL;
793 goto done;
796 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
797 err = got_error_from_errno("asprintf");
798 newlabel = NULL;
799 goto done;
802 if (*p->cid != '\0') {
803 type = "commit";
804 id = p->cid;
805 } else {
806 type = "blob";
807 id = p->blob;
810 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
811 err = got_error_from_errno("asprintf");
812 anclabel = NULL;
813 goto done;
816 err = got_opentemp_named(&mergepath, &mergefile, template);
817 if (err)
818 goto done;
819 outpath = mergepath;
820 outfd = fileno(mergefile);
822 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
823 oldfile, oldlabel, anclabel, newlabel,
824 GOT_DIFF_ALGORITHM_PATIENCE);
825 if (err)
826 goto done;
829 if (nop)
830 goto done;
832 if (p->old != NULL && p->new == NULL) {
833 err = got_worktree_patch_schedule_rm(old, repo, worktree,
834 fileindex, patch_delete, pa);
835 goto done;
838 if (fchmod(outfd, mode) == -1) {
839 err = got_error_from_errno2("chmod", tmppath);
840 goto done;
843 if (rename(outpath, newpath) == -1) {
844 if (errno != ENOENT) {
845 err = got_error_from_errno3("rename", outpath,
846 newpath);
847 goto done;
850 err = got_path_dirname(&parent, newpath);
851 if (err != NULL)
852 goto done;
853 err = got_path_mkdir(parent);
854 if (err != NULL)
855 goto done;
856 if (rename(outpath, newpath) == -1) {
857 err = got_error_from_errno3("rename", outpath,
858 newpath);
859 goto done;
863 if (file_renamed) {
864 err = got_worktree_patch_schedule_rm(old, repo, worktree,
865 fileindex, patch_delete, pa);
866 if (err == NULL)
867 err = got_worktree_patch_schedule_add(new, repo,
868 worktree, fileindex, patch_add,
869 pa);
870 if (err)
871 unlink(newpath);
872 } else if (p->old == NULL) {
873 err = got_worktree_patch_schedule_add(new, repo, worktree,
874 fileindex, patch_add, pa);
875 if (err)
876 unlink(newpath);
877 } else if (*overlapcnt != 0)
878 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
879 else if (do_merge)
880 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
881 else
882 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
884 done:
885 free(parent);
886 free(template);
888 if (tmppath != NULL)
889 unlink(tmppath);
890 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
891 err = got_error_from_errno("fclose");
892 free(tmppath);
894 free(oldpath);
895 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
896 err = got_error_from_errno("fclose");
898 if (apath != NULL)
899 unlink(apath);
900 if (afile != NULL && fclose(afile) == EOF && err == NULL)
901 err = got_error_from_errno("fclose");
902 free(apath);
904 if (mergepath != NULL)
905 unlink(mergepath);
906 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
907 err = got_error_from_errno("fclose");
908 free(mergepath);
910 free(newpath);
911 free(oldlabel);
912 free(newlabel);
913 free(anclabel);
914 return err;
917 static void
918 reverse_patch(struct got_patch *p)
920 struct got_patch_hunk *h;
921 size_t i;
922 int tmp;
924 STAILQ_FOREACH(h, &p->head, entries) {
925 tmp = h->old_from;
926 h->old_from = h->new_from;
927 h->new_from = tmp;
929 tmp = h->old_lines;
930 h->old_lines = h->new_lines;
931 h->new_lines = tmp;
933 tmp = h->old_nonl;
934 h->old_nonl = h->new_nonl;
935 h->new_nonl = tmp;
937 for (i = 0; i < h->len; ++i) {
938 if (*h->lines[i] == '+')
939 *h->lines[i] = '-';
940 else if (*h->lines[i] == '-')
941 *h->lines[i] = '+';
946 const struct got_error *
947 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
948 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
949 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
951 const struct got_error *err = NULL, *complete_err = NULL;
952 struct got_fileindex *fileindex = NULL;
953 char *fileindex_path = NULL;
954 char *oldpath, *newpath;
955 struct imsgbuf *ibuf;
956 int imsg_fds[2] = {-1, -1};
957 int overlapcnt, done = 0, failed = 0;
958 pid_t pid;
960 ibuf = calloc(1, sizeof(*ibuf));
961 if (ibuf == NULL) {
962 err = got_error_from_errno("calloc");
963 goto done;
966 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
967 err = got_error_from_errno("socketpair");
968 goto done;
971 pid = fork();
972 if (pid == -1) {
973 err = got_error_from_errno("fork");
974 goto done;
975 } else if (pid == 0) {
976 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
977 NULL);
978 /* not reached */
981 if (close(imsg_fds[1]) == -1) {
982 err = got_error_from_errno("close");
983 goto done;
985 imsg_fds[1] = -1;
986 imsg_init(ibuf, imsg_fds[0]);
988 err = send_patch(ibuf, fd);
989 fd = -1;
990 if (err)
991 goto done;
993 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
994 worktree);
995 if (err)
996 goto done;
998 while (!done && err == NULL) {
999 struct got_patch p;
1000 struct patch_args pa;
1002 pa.progress_cb = progress_cb;
1003 pa.progress_arg = progress_arg;
1004 pa.head = &p.head;
1006 err = recv_patch(ibuf, &done, &p, strip);
1007 if (err || done)
1008 break;
1010 if (reverse)
1011 reverse_patch(&p);
1013 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1014 &newpath, worktree, repo, fileindex);
1015 if (err == NULL)
1016 err = apply_patch(&overlapcnt, worktree, repo,
1017 fileindex, oldpath, newpath, &p, nop, &pa,
1018 cancel_cb, cancel_arg);
1019 if (err != NULL) {
1020 failed = 1;
1021 /* recoverable errors */
1022 if (err->code == GOT_ERR_FILE_STATUS ||
1023 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1024 err = report_progress(&pa, p.old, p.new,
1025 GOT_STATUS_CANNOT_UPDATE, err);
1026 else if (err->code == GOT_ERR_HUNK_FAILED)
1027 err = report_progress(&pa, p.old, p.new,
1028 GOT_STATUS_CANNOT_UPDATE, NULL);
1030 if (overlapcnt != 0)
1031 failed = 1;
1033 free(oldpath);
1034 free(newpath);
1035 patch_free(&p);
1037 if (err)
1038 break;
1041 done:
1042 if (fileindex != NULL)
1043 complete_err = got_worktree_patch_complete(fileindex,
1044 fileindex_path);
1045 if (complete_err && err == NULL)
1046 err = complete_err;
1047 free(fileindex_path);
1048 if (fd != -1 && close(fd) == -1 && err == NULL)
1049 err = got_error_from_errno("close");
1050 if (ibuf != NULL)
1051 imsg_clear(ibuf);
1052 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1053 err = got_error_from_errno("close");
1054 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1055 err = got_error_from_errno("close");
1056 if (err == NULL && failed)
1057 err = got_error(GOT_ERR_PATCH_FAILED);
1058 return err;