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 return got_privsep_flush_imsg(ibuf);
106 static void
107 patch_free(struct got_patch *p)
109 struct got_patch_hunk *h;
110 size_t i;
112 while (!STAILQ_EMPTY(&p->head)) {
113 h = STAILQ_FIRST(&p->head);
114 STAILQ_REMOVE_HEAD(&p->head, entries);
116 for (i = 0; i < h->len; ++i)
117 free(h->lines[i]);
118 free(h->lines);
119 free(h);
122 free(p->new);
123 free(p->old);
125 memset(p, 0, sizeof(*p));
126 STAILQ_INIT(&p->head);
129 static const struct got_error *
130 pushline(struct got_patch_hunk *h, const char *line)
132 void *t;
133 size_t newcap;
135 if (h->len == h->cap) {
136 if ((newcap = h->cap * 1.5) == 0)
137 newcap = 16;
138 t = recallocarray(h->lines, h->cap, newcap,
139 sizeof(h->lines[0]));
140 if (t == NULL)
141 return got_error_from_errno("recallocarray");
142 h->lines = t;
143 h->cap = newcap;
146 if ((t = strdup(line)) == NULL)
147 return got_error_from_errno("strdup");
149 h->lines[h->len++] = t;
150 return NULL;
153 static const struct got_error *
154 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
156 const struct got_error *err = NULL;
157 struct imsg imsg;
158 struct got_imsg_patch_hunk hdr;
159 struct got_imsg_patch patch;
160 struct got_patch_hunk *h = NULL;
161 size_t datalen;
162 int lastmode = -1;
164 memset(p, 0, sizeof(*p));
165 STAILQ_INIT(&p->head);
167 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
168 if (err)
169 return err;
170 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
171 *done = 1;
172 goto done;
174 if (imsg.hdr.type != GOT_IMSG_PATCH) {
175 err = got_error(GOT_ERR_PRIVSEP_MSG);
176 goto done;
178 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
179 if (datalen != sizeof(patch)) {
180 err = got_error(GOT_ERR_PRIVSEP_LEN);
181 goto done;
183 memcpy(&patch, imsg.data, sizeof(patch));
185 if (patch.old[sizeof(patch.old)-1] != '\0' ||
186 patch.new[sizeof(patch.new)-1] != '\0' ||
187 patch.cid[sizeof(patch.cid)-1] != '\0' ||
188 patch.blob[sizeof(patch.blob)-1] != '\0') {
189 err = got_error(GOT_ERR_PRIVSEP_LEN);
190 goto done;
193 if (*patch.cid != '\0')
194 strlcpy(p->cid, patch.cid, sizeof(p->cid));
196 if (*patch.blob != '\0')
197 strlcpy(p->blob, patch.blob, sizeof(p->blob));
199 /* automatically set strip=1 for git-style diffs */
200 if (strip == -1 && patch.git &&
201 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
202 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
203 strip = 1;
205 /* prefer the new name if not /dev/null for not git-style diffs */
206 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
207 err = got_path_strip(&p->old, patch.new, strip);
208 if (err)
209 goto done;
210 } else if (*patch.old != '\0') {
211 err = got_path_strip(&p->old, patch.old, strip);
212 if (err)
213 goto done;
216 if (*patch.new != '\0') {
217 err = got_path_strip(&p->new, patch.new, strip);
218 if (err)
219 goto done;
222 if (p->old == NULL && p->new == NULL) {
223 err = got_error(GOT_ERR_PATCH_MALFORMED);
224 goto done;
227 imsg_free(&imsg);
229 for (;;) {
230 char *t;
232 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
233 if (err) {
234 patch_free(p);
235 return err;
238 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
239 switch (imsg.hdr.type) {
240 case GOT_IMSG_PATCH_DONE:
241 if (h != NULL && h->len == 0)
242 err = got_error(GOT_ERR_PATCH_MALFORMED);
243 goto done;
244 case GOT_IMSG_PATCH_HUNK:
245 if (h != NULL &&
246 (h->len == 0 || h->old_nonl || h->new_nonl)) {
247 err = got_error(GOT_ERR_PATCH_MALFORMED);
248 goto done;
250 lastmode = -1;
251 if (datalen != sizeof(hdr)) {
252 err = got_error(GOT_ERR_PRIVSEP_LEN);
253 goto done;
255 memcpy(&hdr, imsg.data, sizeof(hdr));
256 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
257 err = got_error(GOT_ERR_PRIVSEP_LEN);
258 goto done;
260 if ((h = calloc(1, sizeof(*h))) == NULL) {
261 err = got_error_from_errno("calloc");
262 goto done;
264 h->old_from = hdr.oldfrom;
265 h->old_lines = hdr.oldlines;
266 h->new_from = hdr.newfrom;
267 h->new_lines = hdr.newlines;
268 STAILQ_INSERT_TAIL(&p->head, h, entries);
269 break;
270 case GOT_IMSG_PATCH_LINE:
271 if (h == NULL) {
272 err = got_error(GOT_ERR_PRIVSEP_MSG);
273 goto done;
275 t = imsg.data;
276 /* at least one char */
277 if (datalen < 2 || t[datalen-1] != '\0') {
278 err = got_error(GOT_ERR_PRIVSEP_MSG);
279 goto done;
281 if (*t != ' ' && *t != '-' && *t != '+' &&
282 *t != '\\') {
283 err = got_error(GOT_ERR_PRIVSEP_MSG);
284 goto done;
287 if (*t != '\\')
288 err = pushline(h, t);
289 else if (lastmode == '-')
290 h->old_nonl = 1;
291 else if (lastmode == '+')
292 h->new_nonl = 1;
293 else
294 err = got_error(GOT_ERR_PATCH_MALFORMED);
296 if (err)
297 goto done;
299 lastmode = *t;
300 break;
301 default:
302 err = got_error(GOT_ERR_PRIVSEP_MSG);
303 goto done;
306 imsg_free(&imsg);
309 done:
310 if (err)
311 patch_free(p);
313 imsg_free(&imsg);
314 return err;
317 static void
318 reverse_patch(struct got_patch *p)
320 struct got_patch_hunk *h;
321 size_t i;
322 int tmp;
324 STAILQ_FOREACH(h, &p->head, entries) {
325 tmp = h->old_from;
326 h->old_from = h->new_from;
327 h->new_from = tmp;
329 tmp = h->old_lines;
330 h->old_lines = h->new_lines;
331 h->new_lines = tmp;
333 tmp = h->old_nonl;
334 h->old_nonl = h->new_nonl;
335 h->new_nonl = tmp;
337 for (i = 0; i < h->len; ++i) {
338 if (*h->lines[i] == '+')
339 *h->lines[i] = '-';
340 else if (*h->lines[i] == '-')
341 *h->lines[i] = '+';
346 /*
347 * Copy data from orig starting at copypos until pos into tmp.
348 * If pos is -1, copy until EOF.
349 */
350 static const struct got_error *
351 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
353 char buf[BUFSIZ];
354 size_t len, r, w;
356 if (fseeko(orig, copypos, SEEK_SET) == -1)
357 return got_error_from_errno("fseeko");
359 while (pos == -1 || copypos < pos) {
360 len = sizeof(buf);
361 if (pos > 0)
362 len = MIN(len, (size_t)pos - copypos);
363 r = fread(buf, 1, len, orig);
364 if (r != len && ferror(orig))
365 return got_error_from_errno("fread");
366 w = fwrite(buf, 1, r, tmp);
367 if (w != r)
368 return got_error_from_errno("fwrite");
369 copypos += len;
370 if (r != len && feof(orig)) {
371 if (pos == -1)
372 return NULL;
373 return got_error(GOT_ERR_HUNK_FAILED);
376 return NULL;
379 static const struct got_error *
380 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
382 const struct got_error *err = NULL;
383 char *line = NULL;
384 char mode = *h->lines[0];
385 size_t linesize = 0;
386 ssize_t linelen;
387 off_t match = -1;
388 int match_lineno = -1;
390 for (;;) {
391 linelen = getline(&line, &linesize, orig);
392 if (linelen == -1) {
393 if (ferror(orig))
394 err = got_error_from_errno("getline");
395 else if (match == -1)
396 err = got_error(GOT_ERR_HUNK_FAILED);
397 break;
399 if (line[linelen - 1] == '\n')
400 line[linelen - 1] = '\0';
401 (*lineno)++;
403 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
404 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
405 (mode == '+' && *lineno == h->old_from)) {
406 match = ftello(orig);
407 if (match == -1) {
408 err = got_error_from_errno("ftello");
409 break;
411 match -= linelen;
412 match_lineno = (*lineno)-1;
415 if (*lineno >= h->old_from && match != -1)
416 break;
419 if (err == NULL) {
420 *pos = match;
421 *lineno = match_lineno;
422 if (fseeko(orig, match, SEEK_SET) == -1)
423 err = got_error_from_errno("fseeko");
426 free(line);
427 return err;
430 static int
431 linecmp(const char *a, const char *b, int *mangled)
433 int c;
435 *mangled = 0;
436 c = strcmp(a, b);
437 if (c == 0)
438 return c;
440 *mangled = 1;
441 for (;;) {
442 while (*a == '\t' || *a == ' ' || *a == '\f')
443 a++;
444 while (*b == '\t' || *b == ' ' || *b == '\f')
445 b++;
446 if (*a == '\0' || *a != *b)
447 break;
448 a++, b++;
451 return *a - *b;
454 static const struct got_error *
455 test_hunk(FILE *orig, struct got_patch_hunk *h)
457 const struct got_error *err = NULL;
458 char *line = NULL;
459 size_t linesize = 0, i = 0;
460 ssize_t linelen;
461 int mangled;
463 for (i = 0; i < h->len; ++i) {
464 switch (*h->lines[i]) {
465 case '+':
466 continue;
467 case ' ':
468 case '-':
469 linelen = getline(&line, &linesize, orig);
470 if (linelen == -1) {
471 if (ferror(orig))
472 err = got_error_from_errno("getline");
473 else
474 err = got_error(
475 GOT_ERR_HUNK_FAILED);
476 goto done;
478 if (line[linelen - 1] == '\n')
479 line[linelen - 1] = '\0';
480 if (linecmp(h->lines[i] + 1, line, &mangled)) {
481 err = got_error(GOT_ERR_HUNK_FAILED);
482 goto done;
484 if (mangled)
485 h->ws_mangled = 1;
486 break;
490 done:
491 free(line);
492 return err;
495 static const struct got_error *
496 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
497 off_t from)
499 const struct got_error *err = NULL;
500 const char *t;
501 size_t linesize = 0, i, new = 0;
502 char *line = NULL;
503 char mode;
504 ssize_t linelen;
506 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
507 return got_error_from_errno("fseeko");
509 for (i = 0; i < h->len; ++i) {
510 switch (mode = *h->lines[i]) {
511 case '-':
512 case ' ':
513 (*lineno)++;
514 if (orig != NULL) {
515 linelen = getline(&line, &linesize, orig);
516 if (linelen == -1) {
517 err = got_error_from_errno("getline");
518 goto done;
520 if (line[linelen - 1] == '\n')
521 line[linelen - 1] = '\0';
522 t = line;
523 } else
524 t = h->lines[i] + 1;
525 if (mode == '-')
526 continue;
527 if (fprintf(tmp, "%s\n", t) < 0) {
528 err = got_error_from_errno("fprintf");
529 goto done;
531 break;
532 case '+':
533 new++;
534 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
535 err = got_error_from_errno("fprintf");
536 goto done;
538 if (new != h->new_lines || !h->new_nonl) {
539 if (fprintf(tmp, "\n") < 0) {
540 err = got_error_from_errno("fprintf");
541 goto done;
544 break;
548 done:
549 free(line);
550 return err;
553 static const struct got_error *
554 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
556 const struct got_error *err = NULL;
557 struct got_patch_hunk *h;
558 struct stat sb;
559 int lineno = 0;
560 off_t copypos, pos;
561 char *line = NULL;
562 size_t linesize = 0;
563 ssize_t linelen;
565 if (p->old == NULL) { /* create */
566 h = STAILQ_FIRST(&p->head);
567 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
568 return got_error(GOT_ERR_PATCH_MALFORMED);
569 return apply_hunk(orig, tmp, h, &lineno, 0);
572 if (fstat(fileno(orig), &sb) == -1)
573 return got_error_from_errno("fstat");
575 copypos = 0;
576 STAILQ_FOREACH(h, &p->head, entries) {
577 tryagain:
578 err = locate_hunk(orig, h, &pos, &lineno);
579 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
580 h->err = err;
581 if (err != NULL)
582 return err;
583 err = copy(tmp, orig, copypos, pos);
584 if (err != NULL)
585 return err;
586 copypos = pos;
588 err = test_hunk(orig, h);
589 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
590 /*
591 * try to apply the hunk again starting the search
592 * after the previous partial match.
593 */
594 if (fseeko(orig, pos, SEEK_SET) == -1)
595 return got_error_from_errno("fseeko");
596 linelen = getline(&line, &linesize, orig);
597 if (linelen == -1)
598 return got_error_from_errno("getline");
599 lineno++;
600 goto tryagain;
602 if (err != NULL)
603 return err;
605 if (lineno + 1 != h->old_from)
606 h->offset = lineno + 1 - h->old_from;
608 err = apply_hunk(orig, tmp, h, &lineno, pos);
609 if (err != NULL)
610 return err;
612 copypos = ftello(orig);
613 if (copypos == -1)
614 return got_error_from_errno("ftello");
617 if (p->new == NULL && sb.st_size != copypos) {
618 h = STAILQ_FIRST(&p->head);
619 h->err = got_error(GOT_ERR_HUNK_FAILED);
620 err = h->err;
621 } else if (!feof(orig))
622 err = copy(tmp, orig, copypos, -1);
624 return err;
627 static const struct got_error *
628 report_progress(struct patch_args *pa, const char *old, const char *new,
629 unsigned char status, const struct got_error *orig_error)
631 const struct got_error *err;
632 struct got_patch_hunk *h;
634 err = pa->progress_cb(pa->progress_arg, old, new, status,
635 orig_error, 0, 0, 0, 0, 0, 0, NULL);
636 if (err)
637 return err;
639 STAILQ_FOREACH(h, pa->head, entries) {
640 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
641 continue;
643 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
644 h->old_from, h->old_lines, h->new_from, h->new_lines,
645 h->offset, h->ws_mangled, h->err);
646 if (err)
647 return err;
650 return NULL;
653 static const struct got_error *
654 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
655 const char *path)
657 return report_progress(arg, path, NULL, status, NULL);
660 static const struct got_error *
661 patch_add(void *arg, unsigned char status, const char *path)
663 return report_progress(arg, NULL, path, status, NULL);
666 static const struct got_error *
667 open_blob(char **path, FILE **fp, const char *blobid,
668 struct got_repository *repo)
670 const struct got_error *err = NULL;
671 struct got_blob_object *blob = NULL;
672 struct got_object_id id, *idptr, *matched_id = NULL;
673 int fd = -1;
675 *fp = NULL;
676 *path = NULL;
678 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
679 err = got_repo_match_object_id(&matched_id, NULL, blobid,
680 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
681 repo);
682 if (err)
683 return err;
684 idptr = matched_id;
685 } else {
686 if (!got_parse_sha1_digest(id.sha1, blobid))
687 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
688 idptr = &id;
691 fd = got_opentempfd();
692 if (fd == -1) {
693 err = got_error_from_errno("got_opentempfd");
694 goto done;
697 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
698 if (err)
699 goto done;
701 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
702 if (err)
703 goto done;
705 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
706 if (err)
707 goto done;
709 done:
710 if (fd != -1 && close(fd) == -1 && err == NULL)
711 err = got_error_from_errno("close");
712 if (blob)
713 got_object_blob_close(blob);
714 if (matched_id != NULL)
715 free(matched_id);
716 if (err) {
717 if (*fp != NULL)
718 fclose(*fp);
719 if (*path != NULL)
720 unlink(*path);
721 free(*path);
722 *fp = NULL;
723 *path = NULL;
725 return err;
728 static const struct got_error *
729 apply_patch(int *overlapcnt, struct got_worktree *worktree,
730 struct got_repository *repo, struct got_fileindex *fileindex,
731 const char *old, const char *new, struct got_patch *p, int nop,
732 int reverse, struct patch_args *pa,
733 got_cancel_cb cancel_cb, void *cancel_arg)
735 const struct got_error *err = NULL;
736 struct stat sb;
737 int do_merge = 0, file_renamed = 0;
738 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
739 char *oldpath = NULL, *newpath = NULL;
740 char *tmppath = NULL, *template = NULL, *parent = NULL;
741 char *apath = NULL, *mergepath = NULL;
742 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
743 int outfd;
744 const char *outpath;
745 mode_t mode = GOT_DEFAULT_FILE_MODE;
747 *overlapcnt = 0;
749 /* don't run the diff3 merge on creations/deletions */
750 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
751 err = open_blob(&apath, &afile, p->blob, repo);
752 /*
753 * ignore failures to open this blob, we might have
754 * parsed gibberish.
755 */
756 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
757 err->code != GOT_ERR_NO_OBJ)
758 return err;
759 else if (err == NULL)
760 do_merge = 1;
761 else if (reverse)
762 reverse_patch(p);
763 err = NULL;
766 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
767 old) == -1) {
768 err = got_error_from_errno("asprintf");
769 goto done;
772 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
773 new) == -1) {
774 err = got_error_from_errno("asprintf");
775 goto done;
778 file_renamed = strcmp(oldpath, newpath);
780 if (asprintf(&template, "%s/got-patch",
781 got_worktree_get_root_path(worktree)) == -1) {
782 err = got_error_from_errno(template);
783 goto done;
786 if (p->old != NULL) {
787 if ((oldfile = fopen(oldpath, "r")) == NULL) {
788 err = got_error_from_errno2("open", oldpath);
789 goto done;
791 if (fstat(fileno(oldfile), &sb) == -1) {
792 err = got_error_from_errno2("fstat", oldpath);
793 goto done;
795 mode = sb.st_mode;
798 err = got_opentemp_named(&tmppath, &tmpfile, template);
799 if (err)
800 goto done;
801 outpath = tmppath;
802 outfd = fileno(tmpfile);
803 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
804 if (err)
805 goto done;
807 if (do_merge) {
808 const char *type, *id;
810 if (fseeko(afile, 0, SEEK_SET) == -1 ||
811 fseeko(oldfile, 0, SEEK_SET) == -1 ||
812 fseeko(tmpfile, 0, SEEK_SET) == -1) {
813 err = got_error_from_errno("fseeko");
814 goto done;
817 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
818 err = got_error_from_errno("asprintf");
819 oldlabel = NULL;
820 goto done;
823 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
824 err = got_error_from_errno("asprintf");
825 newlabel = NULL;
826 goto done;
829 if (*p->cid != '\0') {
830 type = "commit";
831 id = p->cid;
832 } else {
833 type = "blob";
834 id = p->blob;
837 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
838 err = got_error_from_errno("asprintf");
839 anclabel = NULL;
840 goto done;
843 if (reverse) {
844 char *s;
845 FILE *t;
847 s = anclabel;
848 anclabel = newlabel;
849 newlabel = s;
851 t = afile;
852 afile = tmpfile;
853 tmpfile = t;
856 err = got_opentemp_named(&mergepath, &mergefile, template);
857 if (err)
858 goto done;
859 outpath = mergepath;
860 outfd = fileno(mergefile);
862 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
863 oldfile, oldlabel, anclabel, newlabel,
864 GOT_DIFF_ALGORITHM_PATIENCE);
865 if (err)
866 goto done;
869 if (nop)
870 goto done;
872 if (p->old != NULL && p->new == NULL) {
873 err = got_worktree_patch_schedule_rm(old, repo, worktree,
874 fileindex, patch_delete, pa);
875 goto done;
878 if (fchmod(outfd, mode) == -1) {
879 err = got_error_from_errno2("chmod", tmppath);
880 goto done;
883 if (rename(outpath, newpath) == -1) {
884 if (errno != ENOENT) {
885 err = got_error_from_errno3("rename", outpath,
886 newpath);
887 goto done;
890 err = got_path_dirname(&parent, newpath);
891 if (err != NULL)
892 goto done;
893 err = got_path_mkdir(parent);
894 if (err != NULL)
895 goto done;
896 if (rename(outpath, newpath) == -1) {
897 err = got_error_from_errno3("rename", outpath,
898 newpath);
899 goto done;
903 if (file_renamed) {
904 err = got_worktree_patch_schedule_rm(old, repo, worktree,
905 fileindex, patch_delete, pa);
906 if (err == NULL)
907 err = got_worktree_patch_schedule_add(new, repo,
908 worktree, fileindex, patch_add,
909 pa);
910 if (err)
911 unlink(newpath);
912 } else if (p->old == NULL) {
913 err = got_worktree_patch_schedule_add(new, repo, worktree,
914 fileindex, patch_add, pa);
915 if (err)
916 unlink(newpath);
917 } else if (*overlapcnt != 0)
918 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
919 else if (do_merge)
920 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
921 else
922 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
924 done:
925 free(parent);
926 free(template);
928 if (tmppath != NULL)
929 unlink(tmppath);
930 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
931 err = got_error_from_errno("fclose");
932 free(tmppath);
934 free(oldpath);
935 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
936 err = got_error_from_errno("fclose");
938 if (apath != NULL)
939 unlink(apath);
940 if (afile != NULL && fclose(afile) == EOF && err == NULL)
941 err = got_error_from_errno("fclose");
942 free(apath);
944 if (mergepath != NULL)
945 unlink(mergepath);
946 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
947 err = got_error_from_errno("fclose");
948 free(mergepath);
950 free(newpath);
951 free(oldlabel);
952 free(newlabel);
953 free(anclabel);
954 return err;
957 const struct got_error *
958 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
959 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
960 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
962 const struct got_error *err = NULL, *complete_err = NULL;
963 struct got_fileindex *fileindex = NULL;
964 char *fileindex_path = NULL;
965 char *oldpath, *newpath;
966 struct imsgbuf *ibuf;
967 int imsg_fds[2] = {-1, -1};
968 int overlapcnt, done = 0, failed = 0;
969 pid_t pid;
971 ibuf = calloc(1, sizeof(*ibuf));
972 if (ibuf == NULL) {
973 err = got_error_from_errno("calloc");
974 goto done;
977 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
978 err = got_error_from_errno("socketpair");
979 goto done;
982 pid = fork();
983 if (pid == -1) {
984 err = got_error_from_errno("fork");
985 goto done;
986 } else if (pid == 0) {
987 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
988 NULL);
989 /* not reached */
992 if (close(imsg_fds[1]) == -1) {
993 err = got_error_from_errno("close");
994 goto done;
996 imsg_fds[1] = -1;
997 imsg_init(ibuf, imsg_fds[0]);
999 err = send_patch(ibuf, fd);
1000 fd = -1;
1001 if (err)
1002 goto done;
1004 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1005 worktree);
1006 if (err)
1007 goto done;
1009 while (!done && err == NULL) {
1010 struct got_patch p;
1011 struct patch_args pa;
1013 pa.progress_cb = progress_cb;
1014 pa.progress_arg = progress_arg;
1015 pa.head = &p.head;
1017 err = recv_patch(ibuf, &done, &p, strip);
1018 if (err || done)
1019 break;
1021 /* reversal application with merge base is done differently */
1022 if (reverse && *p.blob == '\0')
1023 reverse_patch(&p);
1025 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1026 &newpath, worktree, repo, fileindex);
1027 if (err == NULL)
1028 err = apply_patch(&overlapcnt, worktree, repo,
1029 fileindex, oldpath, newpath, &p, nop, reverse,
1030 &pa, cancel_cb, cancel_arg);
1031 if (err != NULL) {
1032 failed = 1;
1033 /* recoverable errors */
1034 if (err->code == GOT_ERR_FILE_STATUS ||
1035 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1036 err = report_progress(&pa, p.old, p.new,
1037 GOT_STATUS_CANNOT_UPDATE, err);
1038 else if (err->code == GOT_ERR_HUNK_FAILED)
1039 err = report_progress(&pa, p.old, p.new,
1040 GOT_STATUS_CANNOT_UPDATE, NULL);
1042 if (overlapcnt != 0)
1043 failed = 1;
1045 free(oldpath);
1046 free(newpath);
1047 patch_free(&p);
1049 if (err)
1050 break;
1053 done:
1054 if (fileindex != NULL)
1055 complete_err = got_worktree_patch_complete(fileindex,
1056 fileindex_path);
1057 if (complete_err && err == NULL)
1058 err = complete_err;
1059 free(fileindex_path);
1060 if (fd != -1 && close(fd) == -1 && err == NULL)
1061 err = got_error_from_errno("close");
1062 if (ibuf != NULL)
1063 imsg_clear(ibuf);
1064 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1065 err = got_error_from_errno("close");
1066 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1067 err = got_error_from_errno("close");
1068 if (err == NULL && failed)
1069 err = got_error(GOT_ERR_PATCH_FAILED);
1070 return err;