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 static void
316 reverse_patch(struct got_patch *p)
318 struct got_patch_hunk *h;
319 size_t i;
320 int tmp;
322 STAILQ_FOREACH(h, &p->head, entries) {
323 tmp = h->old_from;
324 h->old_from = h->new_from;
325 h->new_from = tmp;
327 tmp = h->old_lines;
328 h->old_lines = h->new_lines;
329 h->new_lines = tmp;
331 tmp = h->old_nonl;
332 h->old_nonl = h->new_nonl;
333 h->new_nonl = tmp;
335 for (i = 0; i < h->len; ++i) {
336 if (*h->lines[i] == '+')
337 *h->lines[i] = '-';
338 else if (*h->lines[i] == '-')
339 *h->lines[i] = '+';
344 /*
345 * Copy data from orig starting at copypos until pos into tmp.
346 * If pos is -1, copy until EOF.
347 */
348 static const struct got_error *
349 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
351 char buf[BUFSIZ];
352 size_t len, r, w;
354 if (fseeko(orig, copypos, SEEK_SET) == -1)
355 return got_error_from_errno("fseeko");
357 while (pos == -1 || copypos < pos) {
358 len = sizeof(buf);
359 if (pos > 0)
360 len = MIN(len, (size_t)pos - copypos);
361 r = fread(buf, 1, len, orig);
362 if (r != len && ferror(orig))
363 return got_error_from_errno("fread");
364 w = fwrite(buf, 1, r, tmp);
365 if (w != r)
366 return got_error_from_errno("fwrite");
367 copypos += len;
368 if (r != len && feof(orig)) {
369 if (pos == -1)
370 return NULL;
371 return got_error(GOT_ERR_HUNK_FAILED);
374 return NULL;
377 static const struct got_error *
378 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
380 const struct got_error *err = NULL;
381 char *line = NULL;
382 char mode = *h->lines[0];
383 size_t linesize = 0;
384 ssize_t linelen;
385 off_t match = -1;
386 int match_lineno = -1;
388 for (;;) {
389 linelen = getline(&line, &linesize, orig);
390 if (linelen == -1) {
391 if (ferror(orig))
392 err = got_error_from_errno("getline");
393 else if (match == -1)
394 err = got_error(GOT_ERR_HUNK_FAILED);
395 break;
397 if (line[linelen - 1] == '\n')
398 line[linelen - 1] = '\0';
399 (*lineno)++;
401 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
402 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
403 (mode == '+' && *lineno == h->old_from)) {
404 match = ftello(orig);
405 if (match == -1) {
406 err = got_error_from_errno("ftello");
407 break;
409 match -= linelen;
410 match_lineno = (*lineno)-1;
413 if (*lineno >= h->old_from && match != -1)
414 break;
417 if (err == NULL) {
418 *pos = match;
419 *lineno = match_lineno;
420 if (fseeko(orig, match, SEEK_SET) == -1)
421 err = got_error_from_errno("fseeko");
424 free(line);
425 return err;
428 static int
429 linecmp(const char *a, const char *b, int *mangled)
431 int c;
433 *mangled = 0;
434 c = strcmp(a, b);
435 if (c == 0)
436 return c;
438 *mangled = 1;
439 for (;;) {
440 while (*a == '\t' || *a == ' ' || *a == '\f')
441 a++;
442 while (*b == '\t' || *b == ' ' || *b == '\f')
443 b++;
444 if (*a == '\0' || *a != *b)
445 break;
446 a++, b++;
449 return *a - *b;
452 static const struct got_error *
453 test_hunk(FILE *orig, struct got_patch_hunk *h)
455 const struct got_error *err = NULL;
456 char *line = NULL;
457 size_t linesize = 0, i = 0;
458 ssize_t linelen;
459 int mangled;
461 for (i = 0; i < h->len; ++i) {
462 switch (*h->lines[i]) {
463 case '+':
464 continue;
465 case ' ':
466 case '-':
467 linelen = getline(&line, &linesize, orig);
468 if (linelen == -1) {
469 if (ferror(orig))
470 err = got_error_from_errno("getline");
471 else
472 err = got_error(
473 GOT_ERR_HUNK_FAILED);
474 goto done;
476 if (line[linelen - 1] == '\n')
477 line[linelen - 1] = '\0';
478 if (linecmp(h->lines[i] + 1, line, &mangled)) {
479 err = got_error(GOT_ERR_HUNK_FAILED);
480 goto done;
482 if (mangled)
483 h->ws_mangled = 1;
484 break;
488 done:
489 free(line);
490 return err;
493 static const struct got_error *
494 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
495 off_t from)
497 const struct got_error *err = NULL;
498 const char *t;
499 size_t linesize = 0, i, new = 0;
500 char *line = NULL;
501 char mode;
502 ssize_t linelen;
504 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
505 return got_error_from_errno("fseeko");
507 for (i = 0; i < h->len; ++i) {
508 switch (mode = *h->lines[i]) {
509 case '-':
510 case ' ':
511 (*lineno)++;
512 if (orig != NULL) {
513 linelen = getline(&line, &linesize, orig);
514 if (linelen == -1) {
515 err = got_error_from_errno("getline");
516 goto done;
518 if (line[linelen - 1] == '\n')
519 line[linelen - 1] = '\0';
520 t = line;
521 } else
522 t = h->lines[i] + 1;
523 if (mode == '-')
524 continue;
525 if (fprintf(tmp, "%s\n", t) < 0) {
526 err = got_error_from_errno("fprintf");
527 goto done;
529 break;
530 case '+':
531 new++;
532 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
533 err = got_error_from_errno("fprintf");
534 goto done;
536 if (new != h->new_lines || !h->new_nonl) {
537 if (fprintf(tmp, "\n") < 0) {
538 err = got_error_from_errno("fprintf");
539 goto done;
542 break;
546 done:
547 free(line);
548 return err;
551 static const struct got_error *
552 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
554 const struct got_error *err = NULL;
555 struct got_patch_hunk *h;
556 struct stat sb;
557 int lineno = 0;
558 off_t copypos, pos;
559 char *line = NULL;
560 size_t linesize = 0;
561 ssize_t linelen;
563 if (p->old == NULL) { /* create */
564 h = STAILQ_FIRST(&p->head);
565 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
566 return got_error(GOT_ERR_PATCH_MALFORMED);
567 return apply_hunk(orig, tmp, h, &lineno, 0);
570 if (fstat(fileno(orig), &sb) == -1)
571 return got_error_from_errno("fstat");
573 copypos = 0;
574 STAILQ_FOREACH(h, &p->head, entries) {
575 tryagain:
576 err = locate_hunk(orig, h, &pos, &lineno);
577 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
578 h->err = err;
579 if (err != NULL)
580 return err;
581 err = copy(tmp, orig, copypos, pos);
582 if (err != NULL)
583 return err;
584 copypos = pos;
586 err = test_hunk(orig, h);
587 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
588 /*
589 * try to apply the hunk again starting the search
590 * after the previous partial match.
591 */
592 if (fseeko(orig, pos, SEEK_SET) == -1)
593 return got_error_from_errno("fseeko");
594 linelen = getline(&line, &linesize, orig);
595 if (linelen == -1)
596 return got_error_from_errno("getline");
597 lineno++;
598 goto tryagain;
600 if (err != NULL)
601 return err;
603 if (lineno + 1 != h->old_from)
604 h->offset = lineno + 1 - h->old_from;
606 err = apply_hunk(orig, tmp, h, &lineno, pos);
607 if (err != NULL)
608 return err;
610 copypos = ftello(orig);
611 if (copypos == -1)
612 return got_error_from_errno("ftello");
615 if (p->new == NULL && sb.st_size != copypos) {
616 h = STAILQ_FIRST(&p->head);
617 h->err = got_error(GOT_ERR_HUNK_FAILED);
618 err = h->err;
619 } else if (!feof(orig))
620 err = copy(tmp, orig, copypos, -1);
622 return err;
625 static const struct got_error *
626 report_progress(struct patch_args *pa, const char *old, const char *new,
627 unsigned char status, const struct got_error *orig_error)
629 const struct got_error *err;
630 struct got_patch_hunk *h;
632 err = pa->progress_cb(pa->progress_arg, old, new, status,
633 orig_error, 0, 0, 0, 0, 0, 0, NULL);
634 if (err)
635 return err;
637 STAILQ_FOREACH(h, pa->head, entries) {
638 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
639 continue;
641 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
642 h->old_from, h->old_lines, h->new_from, h->new_lines,
643 h->offset, h->ws_mangled, h->err);
644 if (err)
645 return err;
648 return NULL;
651 static const struct got_error *
652 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
653 const char *path)
655 return report_progress(arg, path, NULL, status, NULL);
658 static const struct got_error *
659 patch_add(void *arg, unsigned char status, const char *path)
661 return report_progress(arg, NULL, path, status, NULL);
664 static const struct got_error *
665 open_blob(char **path, FILE **fp, const char *blobid,
666 struct got_repository *repo)
668 const struct got_error *err = NULL;
669 struct got_blob_object *blob = NULL;
670 struct got_object_id id, *idptr, *matched_id = NULL;
671 int fd = -1;
673 *fp = NULL;
674 *path = NULL;
676 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
677 err = got_repo_match_object_id(&matched_id, NULL, blobid,
678 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
679 repo);
680 if (err)
681 return err;
682 idptr = matched_id;
683 } else {
684 if (!got_parse_sha1_digest(id.sha1, blobid))
685 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
686 idptr = &id;
689 fd = got_opentempfd();
690 if (fd == -1) {
691 err = got_error_from_errno("got_opentempfd");
692 goto done;
695 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
696 if (err)
697 goto done;
699 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
700 if (err)
701 goto done;
703 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
704 if (err)
705 goto done;
707 done:
708 if (fd != -1 && close(fd) == -1 && err == NULL)
709 err = got_error_from_errno("close");
710 if (blob)
711 got_object_blob_close(blob);
712 if (matched_id != NULL)
713 free(matched_id);
714 if (err) {
715 if (*fp != NULL)
716 fclose(*fp);
717 if (*path != NULL)
718 unlink(*path);
719 free(*path);
720 *fp = NULL;
721 *path = NULL;
723 return err;
726 static const struct got_error *
727 apply_patch(int *overlapcnt, struct got_worktree *worktree,
728 struct got_repository *repo, struct got_fileindex *fileindex,
729 const char *old, const char *new, struct got_patch *p, int nop,
730 int reverse, struct patch_args *pa,
731 got_cancel_cb cancel_cb, void *cancel_arg)
733 const struct got_error *err = NULL;
734 struct stat sb;
735 int do_merge = 0, file_renamed = 0;
736 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
737 char *oldpath = NULL, *newpath = NULL;
738 char *tmppath = NULL, *template = NULL, *parent = NULL;
739 char *apath = NULL, *mergepath = NULL;
740 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
741 int outfd;
742 const char *outpath;
743 mode_t mode = GOT_DEFAULT_FILE_MODE;
745 *overlapcnt = 0;
747 /* don't run the diff3 merge on creations/deletions */
748 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
749 err = open_blob(&apath, &afile, p->blob, repo);
750 /*
751 * ignore failures to open this blob, we might have
752 * parsed gibberish.
753 */
754 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
755 err->code != GOT_ERR_NO_OBJ)
756 return err;
757 else if (err == NULL)
758 do_merge = 1;
759 else if (reverse)
760 reverse_patch(p);
761 err = NULL;
764 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
765 old) == -1) {
766 err = got_error_from_errno("asprintf");
767 goto done;
770 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
771 new) == -1) {
772 err = got_error_from_errno("asprintf");
773 goto done;
776 file_renamed = strcmp(oldpath, newpath);
778 if (asprintf(&template, "%s/got-patch",
779 got_worktree_get_root_path(worktree)) == -1) {
780 err = got_error_from_errno(template);
781 goto done;
784 if (p->old != NULL) {
785 if ((oldfile = fopen(oldpath, "r")) == NULL) {
786 err = got_error_from_errno2("open", oldpath);
787 goto done;
789 if (fstat(fileno(oldfile), &sb) == -1) {
790 err = got_error_from_errno2("fstat", oldpath);
791 goto done;
793 mode = sb.st_mode;
796 err = got_opentemp_named(&tmppath, &tmpfile, template);
797 if (err)
798 goto done;
799 outpath = tmppath;
800 outfd = fileno(tmpfile);
801 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
802 if (err)
803 goto done;
805 if (do_merge) {
806 const char *type, *id;
808 if (fseeko(afile, 0, SEEK_SET) == -1 ||
809 fseeko(oldfile, 0, SEEK_SET) == -1 ||
810 fseeko(tmpfile, 0, SEEK_SET) == -1) {
811 err = got_error_from_errno("fseeko");
812 goto done;
815 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
816 err = got_error_from_errno("asprintf");
817 oldlabel = NULL;
818 goto done;
821 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
822 err = got_error_from_errno("asprintf");
823 newlabel = NULL;
824 goto done;
827 if (*p->cid != '\0') {
828 type = "commit";
829 id = p->cid;
830 } else {
831 type = "blob";
832 id = p->blob;
835 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
836 err = got_error_from_errno("asprintf");
837 anclabel = NULL;
838 goto done;
841 if (reverse) {
842 char *s;
843 FILE *t;
845 s = anclabel;
846 anclabel = newlabel;
847 newlabel = s;
849 t = afile;
850 afile = tmpfile;
851 tmpfile = t;
854 err = got_opentemp_named(&mergepath, &mergefile, template);
855 if (err)
856 goto done;
857 outpath = mergepath;
858 outfd = fileno(mergefile);
860 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
861 oldfile, oldlabel, anclabel, newlabel,
862 GOT_DIFF_ALGORITHM_PATIENCE);
863 if (err)
864 goto done;
867 if (nop)
868 goto done;
870 if (p->old != NULL && p->new == NULL) {
871 err = got_worktree_patch_schedule_rm(old, repo, worktree,
872 fileindex, patch_delete, pa);
873 goto done;
876 if (fchmod(outfd, mode) == -1) {
877 err = got_error_from_errno2("chmod", tmppath);
878 goto done;
881 if (rename(outpath, newpath) == -1) {
882 if (errno != ENOENT) {
883 err = got_error_from_errno3("rename", outpath,
884 newpath);
885 goto done;
888 err = got_path_dirname(&parent, newpath);
889 if (err != NULL)
890 goto done;
891 err = got_path_mkdir(parent);
892 if (err != NULL)
893 goto done;
894 if (rename(outpath, newpath) == -1) {
895 err = got_error_from_errno3("rename", outpath,
896 newpath);
897 goto done;
901 if (file_renamed) {
902 err = got_worktree_patch_schedule_rm(old, repo, worktree,
903 fileindex, patch_delete, pa);
904 if (err == NULL)
905 err = got_worktree_patch_schedule_add(new, repo,
906 worktree, fileindex, patch_add,
907 pa);
908 if (err)
909 unlink(newpath);
910 } else if (p->old == NULL) {
911 err = got_worktree_patch_schedule_add(new, repo, worktree,
912 fileindex, patch_add, pa);
913 if (err)
914 unlink(newpath);
915 } else if (*overlapcnt != 0)
916 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
917 else if (do_merge)
918 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
919 else
920 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
922 done:
923 free(parent);
924 free(template);
926 if (tmppath != NULL)
927 unlink(tmppath);
928 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
929 err = got_error_from_errno("fclose");
930 free(tmppath);
932 free(oldpath);
933 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
934 err = got_error_from_errno("fclose");
936 if (apath != NULL)
937 unlink(apath);
938 if (afile != NULL && fclose(afile) == EOF && err == NULL)
939 err = got_error_from_errno("fclose");
940 free(apath);
942 if (mergepath != NULL)
943 unlink(mergepath);
944 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
945 err = got_error_from_errno("fclose");
946 free(mergepath);
948 free(newpath);
949 free(oldlabel);
950 free(newlabel);
951 free(anclabel);
952 return err;
955 const struct got_error *
956 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
957 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
958 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
960 const struct got_error *err = NULL, *complete_err = NULL;
961 struct got_fileindex *fileindex = NULL;
962 char *fileindex_path = NULL;
963 char *oldpath, *newpath;
964 struct imsgbuf *ibuf;
965 int imsg_fds[2] = {-1, -1};
966 int overlapcnt, done = 0, failed = 0;
967 pid_t pid;
969 ibuf = calloc(1, sizeof(*ibuf));
970 if (ibuf == NULL) {
971 err = got_error_from_errno("calloc");
972 goto done;
975 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
976 err = got_error_from_errno("socketpair");
977 goto done;
980 pid = fork();
981 if (pid == -1) {
982 err = got_error_from_errno("fork");
983 goto done;
984 } else if (pid == 0) {
985 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
986 NULL);
987 /* not reached */
990 if (close(imsg_fds[1]) == -1) {
991 err = got_error_from_errno("close");
992 goto done;
994 imsg_fds[1] = -1;
995 imsg_init(ibuf, imsg_fds[0]);
997 err = send_patch(ibuf, fd);
998 fd = -1;
999 if (err)
1000 goto done;
1002 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1003 worktree);
1004 if (err)
1005 goto done;
1007 while (!done && err == NULL) {
1008 struct got_patch p;
1009 struct patch_args pa;
1011 pa.progress_cb = progress_cb;
1012 pa.progress_arg = progress_arg;
1013 pa.head = &p.head;
1015 err = recv_patch(ibuf, &done, &p, strip);
1016 if (err || done)
1017 break;
1019 /* reversal application with merge base is done differently */
1020 if (reverse && *p.blob == '\0')
1021 reverse_patch(&p);
1023 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1024 &newpath, worktree, repo, fileindex);
1025 if (err == NULL)
1026 err = apply_patch(&overlapcnt, worktree, repo,
1027 fileindex, oldpath, newpath, &p, nop, reverse,
1028 &pa, cancel_cb, cancel_arg);
1029 if (err != NULL) {
1030 failed = 1;
1031 /* recoverable errors */
1032 if (err->code == GOT_ERR_FILE_STATUS ||
1033 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1034 err = report_progress(&pa, p.old, p.new,
1035 GOT_STATUS_CANNOT_UPDATE, err);
1036 else if (err->code == GOT_ERR_HUNK_FAILED)
1037 err = report_progress(&pa, p.old, p.new,
1038 GOT_STATUS_CANNOT_UPDATE, NULL);
1040 if (overlapcnt != 0)
1041 failed = 1;
1043 free(oldpath);
1044 free(newpath);
1045 patch_free(&p);
1047 if (err)
1048 break;
1051 done:
1052 if (fileindex != NULL)
1053 complete_err = got_worktree_patch_complete(fileindex,
1054 fileindex_path);
1055 if (complete_err && err == NULL)
1056 err = complete_err;
1057 free(fileindex_path);
1058 if (fd != -1 && close(fd) == -1 && err == NULL)
1059 err = got_error_from_errno("close");
1060 if (ibuf != NULL)
1061 imsg_clear(ibuf);
1062 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1063 err = got_error_from_errno("close");
1064 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1065 err = got_error_from_errno("close");
1066 if (err == NULL && failed)
1067 err = got_error(GOT_ERR_PATCH_FAILED);
1068 return err;