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 int xbit;
78 char *old;
79 char *new;
80 char cid[41];
81 char blob[41];
82 struct got_patch_hunk_head head;
83 };
85 struct patch_args {
86 got_patch_progress_cb progress_cb;
87 void *progress_arg;
88 struct got_patch_hunk_head *head;
89 };
91 static mode_t
92 apply_umask(mode_t mode)
93 {
94 mode_t um;
96 um = umask(000);
97 umask(um);
98 return mode & ~um;
99 }
101 static const struct got_error *
102 send_patch(struct imsgbuf *ibuf, int fd)
104 const struct got_error *err = NULL;
106 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
107 NULL, 0) == -1) {
108 err = got_error_from_errno(
109 "imsg_compose GOT_IMSG_PATCH_FILE");
110 close(fd);
111 return err;
114 return got_privsep_flush_imsg(ibuf);
117 static void
118 patch_free(struct got_patch *p)
120 struct got_patch_hunk *h;
121 size_t i;
123 while (!STAILQ_EMPTY(&p->head)) {
124 h = STAILQ_FIRST(&p->head);
125 STAILQ_REMOVE_HEAD(&p->head, entries);
127 for (i = 0; i < h->len; ++i)
128 free(h->lines[i]);
129 free(h->lines);
130 free(h);
133 free(p->new);
134 free(p->old);
136 memset(p, 0, sizeof(*p));
137 STAILQ_INIT(&p->head);
140 static const struct got_error *
141 pushline(struct got_patch_hunk *h, const char *line)
143 void *t;
144 size_t newcap;
146 if (h->len == h->cap) {
147 if ((newcap = h->cap * 1.5) == 0)
148 newcap = 16;
149 t = recallocarray(h->lines, h->cap, newcap,
150 sizeof(h->lines[0]));
151 if (t == NULL)
152 return got_error_from_errno("recallocarray");
153 h->lines = t;
154 h->cap = newcap;
157 if ((t = strdup(line)) == NULL)
158 return got_error_from_errno("strdup");
160 h->lines[h->len++] = t;
161 return NULL;
164 static const struct got_error *
165 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
167 const struct got_error *err = NULL;
168 struct imsg imsg;
169 struct got_imsg_patch_hunk hdr;
170 struct got_imsg_patch patch;
171 struct got_patch_hunk *h = NULL;
172 size_t datalen;
173 int lastmode = -1;
175 memset(p, 0, sizeof(*p));
176 STAILQ_INIT(&p->head);
178 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
179 if (err)
180 return err;
181 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
182 *done = 1;
183 goto done;
185 if (imsg.hdr.type != GOT_IMSG_PATCH) {
186 err = got_error(GOT_ERR_PRIVSEP_MSG);
187 goto done;
189 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
190 if (datalen != sizeof(patch)) {
191 err = got_error(GOT_ERR_PRIVSEP_LEN);
192 goto done;
194 memcpy(&patch, imsg.data, sizeof(patch));
196 if (patch.old[sizeof(patch.old)-1] != '\0' ||
197 patch.new[sizeof(patch.new)-1] != '\0' ||
198 patch.cid[sizeof(patch.cid)-1] != '\0' ||
199 patch.blob[sizeof(patch.blob)-1] != '\0') {
200 err = got_error(GOT_ERR_PRIVSEP_LEN);
201 goto done;
204 if (*patch.cid != '\0')
205 strlcpy(p->cid, patch.cid, sizeof(p->cid));
207 if (*patch.blob != '\0')
208 strlcpy(p->blob, patch.blob, sizeof(p->blob));
210 p->xbit = patch.xbit;
212 /* automatically set strip=1 for git-style diffs */
213 if (strip == -1 && patch.git &&
214 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
215 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
216 strip = 1;
218 /* prefer the new name if not /dev/null for not git-style diffs */
219 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
220 err = got_path_strip(&p->old, patch.new, strip);
221 if (err)
222 goto done;
223 } else if (*patch.old != '\0') {
224 err = got_path_strip(&p->old, patch.old, strip);
225 if (err)
226 goto done;
229 if (*patch.new != '\0') {
230 err = got_path_strip(&p->new, patch.new, strip);
231 if (err)
232 goto done;
235 if (p->old == NULL && p->new == NULL) {
236 err = got_error(GOT_ERR_PATCH_MALFORMED);
237 goto done;
240 imsg_free(&imsg);
242 for (;;) {
243 char *t;
245 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
246 if (err) {
247 patch_free(p);
248 return err;
251 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
252 switch (imsg.hdr.type) {
253 case GOT_IMSG_PATCH_DONE:
254 if (h != NULL && h->len == 0)
255 err = got_error(GOT_ERR_PATCH_MALFORMED);
256 goto done;
257 case GOT_IMSG_PATCH_HUNK:
258 if (h != NULL &&
259 (h->len == 0 || h->old_nonl || h->new_nonl)) {
260 err = got_error(GOT_ERR_PATCH_MALFORMED);
261 goto done;
263 lastmode = -1;
264 if (datalen != sizeof(hdr)) {
265 err = got_error(GOT_ERR_PRIVSEP_LEN);
266 goto done;
268 memcpy(&hdr, imsg.data, sizeof(hdr));
269 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
270 err = got_error(GOT_ERR_PRIVSEP_LEN);
271 goto done;
273 if ((h = calloc(1, sizeof(*h))) == NULL) {
274 err = got_error_from_errno("calloc");
275 goto done;
277 h->old_from = hdr.oldfrom;
278 h->old_lines = hdr.oldlines;
279 h->new_from = hdr.newfrom;
280 h->new_lines = hdr.newlines;
281 STAILQ_INSERT_TAIL(&p->head, h, entries);
282 break;
283 case GOT_IMSG_PATCH_LINE:
284 if (h == NULL) {
285 err = got_error(GOT_ERR_PRIVSEP_MSG);
286 goto done;
288 t = imsg.data;
289 /* at least one char */
290 if (datalen < 2 || t[datalen-1] != '\0') {
291 err = got_error(GOT_ERR_PRIVSEP_MSG);
292 goto done;
294 if (*t != ' ' && *t != '-' && *t != '+' &&
295 *t != '\\') {
296 err = got_error(GOT_ERR_PRIVSEP_MSG);
297 goto done;
300 if (*t != '\\')
301 err = pushline(h, t);
302 else if (lastmode == '-')
303 h->old_nonl = 1;
304 else if (lastmode == '+')
305 h->new_nonl = 1;
306 else
307 err = got_error(GOT_ERR_PATCH_MALFORMED);
309 if (err)
310 goto done;
312 lastmode = *t;
313 break;
314 default:
315 err = got_error(GOT_ERR_PRIVSEP_MSG);
316 goto done;
319 imsg_free(&imsg);
322 done:
323 if (err)
324 patch_free(p);
326 imsg_free(&imsg);
327 return err;
330 static void
331 reverse_patch(struct got_patch *p)
333 struct got_patch_hunk *h;
334 size_t i;
335 int tmp;
337 STAILQ_FOREACH(h, &p->head, entries) {
338 tmp = h->old_from;
339 h->old_from = h->new_from;
340 h->new_from = tmp;
342 tmp = h->old_lines;
343 h->old_lines = h->new_lines;
344 h->new_lines = tmp;
346 tmp = h->old_nonl;
347 h->old_nonl = h->new_nonl;
348 h->new_nonl = tmp;
350 for (i = 0; i < h->len; ++i) {
351 if (*h->lines[i] == '+')
352 *h->lines[i] = '-';
353 else if (*h->lines[i] == '-')
354 *h->lines[i] = '+';
359 /*
360 * Copy data from orig starting at copypos until pos into tmp.
361 * If pos is -1, copy until EOF.
362 */
363 static const struct got_error *
364 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
366 char buf[BUFSIZ];
367 size_t len, r, w;
369 if (fseeko(orig, copypos, SEEK_SET) == -1)
370 return got_error_from_errno("fseeko");
372 while (pos == -1 || copypos < pos) {
373 len = sizeof(buf);
374 if (pos > 0)
375 len = MIN(len, (size_t)pos - copypos);
376 r = fread(buf, 1, len, orig);
377 if (r != len && ferror(orig))
378 return got_error_from_errno("fread");
379 w = fwrite(buf, 1, r, tmp);
380 if (w != r)
381 return got_error_from_errno("fwrite");
382 copypos += len;
383 if (r != len && feof(orig)) {
384 if (pos == -1)
385 return NULL;
386 return got_error(GOT_ERR_HUNK_FAILED);
389 return NULL;
392 static int linecmp(const char *, const char *, int *);
394 static const struct got_error *
395 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
397 const struct got_error *err = NULL;
398 char *line = NULL;
399 char mode = *h->lines[0];
400 size_t linesize = 0;
401 ssize_t linelen;
402 off_t match = -1;
403 int mangled = 0, match_lineno = -1;
405 for (;;) {
406 linelen = getline(&line, &linesize, orig);
407 if (linelen == -1) {
408 if (ferror(orig))
409 err = got_error_from_errno("getline");
410 else if (match == -1)
411 err = got_error(GOT_ERR_HUNK_FAILED);
412 break;
414 if (line[linelen - 1] == '\n')
415 line[linelen - 1] = '\0';
416 (*lineno)++;
418 if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
419 (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
420 (mode == '+' && *lineno == h->old_from)) {
421 match = ftello(orig);
422 if (match == -1) {
423 err = got_error_from_errno("ftello");
424 break;
426 match -= linelen;
427 match_lineno = (*lineno)-1;
430 if (*lineno >= h->old_from && match != -1) {
431 if (mangled)
432 h->ws_mangled = 1;
433 break;
437 if (err == NULL) {
438 *pos = match;
439 *lineno = match_lineno;
440 if (fseeko(orig, match, SEEK_SET) == -1)
441 err = got_error_from_errno("fseeko");
444 free(line);
445 return err;
448 static int
449 linecmp(const char *a, const char *b, int *mangled)
451 int c;
453 *mangled = 0;
454 c = strcmp(a, b);
455 if (c == 0)
456 return c;
458 *mangled = 1;
459 for (;;) {
460 while (*a == '\t' || *a == ' ' || *a == '\f')
461 a++;
462 while (*b == '\t' || *b == ' ' || *b == '\f')
463 b++;
464 if (*a == '\0' || *a != *b)
465 break;
466 a++, b++;
469 return *a - *b;
472 static const struct got_error *
473 test_hunk(FILE *orig, struct got_patch_hunk *h)
475 const struct got_error *err = NULL;
476 char *line = NULL;
477 size_t linesize = 0, i = 0;
478 ssize_t linelen;
479 int mangled;
481 for (i = 0; i < h->len; ++i) {
482 switch (*h->lines[i]) {
483 case '+':
484 continue;
485 case ' ':
486 case '-':
487 linelen = getline(&line, &linesize, orig);
488 if (linelen == -1) {
489 if (ferror(orig))
490 err = got_error_from_errno("getline");
491 else
492 err = got_error(
493 GOT_ERR_HUNK_FAILED);
494 goto done;
496 if (line[linelen - 1] == '\n')
497 line[linelen - 1] = '\0';
498 if (linecmp(h->lines[i] + 1, line, &mangled)) {
499 err = got_error(GOT_ERR_HUNK_FAILED);
500 goto done;
502 if (mangled)
503 h->ws_mangled = 1;
504 break;
508 done:
509 free(line);
510 return err;
513 static const struct got_error *
514 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
515 off_t from)
517 const struct got_error *err = NULL;
518 const char *t;
519 size_t linesize = 0, i, new = 0;
520 char *line = NULL;
521 char mode;
522 ssize_t linelen;
524 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
525 return got_error_from_errno("fseeko");
527 for (i = 0; i < h->len; ++i) {
528 switch (mode = *h->lines[i]) {
529 case '-':
530 case ' ':
531 (*lineno)++;
532 if (orig != NULL) {
533 linelen = getline(&line, &linesize, orig);
534 if (linelen == -1) {
535 err = got_error_from_errno("getline");
536 goto done;
538 if (line[linelen - 1] == '\n')
539 line[linelen - 1] = '\0';
540 t = line;
541 } else
542 t = h->lines[i] + 1;
543 if (mode == '-')
544 continue;
545 if (fprintf(tmp, "%s\n", t) < 0) {
546 err = got_error_from_errno("fprintf");
547 goto done;
549 break;
550 case '+':
551 new++;
552 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
553 err = got_error_from_errno("fprintf");
554 goto done;
556 if (new != h->new_lines || !h->new_nonl) {
557 if (fprintf(tmp, "\n") < 0) {
558 err = got_error_from_errno("fprintf");
559 goto done;
562 break;
566 done:
567 free(line);
568 return err;
571 static const struct got_error *
572 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
574 const struct got_error *err = NULL;
575 struct got_patch_hunk *h;
576 struct stat sb;
577 int lineno = 0;
578 off_t copypos, pos;
579 char *line = NULL;
580 size_t linesize = 0;
581 ssize_t linelen;
583 if (p->old == NULL) { /* create */
584 h = STAILQ_FIRST(&p->head);
585 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
586 return got_error(GOT_ERR_PATCH_MALFORMED);
587 return apply_hunk(orig, tmp, h, &lineno, 0);
590 if (fstat(fileno(orig), &sb) == -1)
591 return got_error_from_errno("fstat");
593 copypos = 0;
594 STAILQ_FOREACH(h, &p->head, entries) {
595 tryagain:
596 err = locate_hunk(orig, h, &pos, &lineno);
597 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
598 h->err = err;
599 if (err != NULL)
600 return err;
601 err = copy(tmp, orig, copypos, pos);
602 if (err != NULL)
603 return err;
604 copypos = pos;
606 err = test_hunk(orig, h);
607 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
608 /*
609 * try to apply the hunk again starting the search
610 * after the previous partial match.
611 */
612 if (fseeko(orig, pos, SEEK_SET) == -1)
613 return got_error_from_errno("fseeko");
614 linelen = getline(&line, &linesize, orig);
615 if (linelen == -1)
616 return got_error_from_errno("getline");
617 lineno++;
618 goto tryagain;
620 if (err != NULL)
621 return err;
623 if (lineno + 1 != h->old_from)
624 h->offset = lineno + 1 - h->old_from;
626 err = apply_hunk(orig, tmp, h, &lineno, pos);
627 if (err != NULL)
628 return err;
630 copypos = ftello(orig);
631 if (copypos == -1)
632 return got_error_from_errno("ftello");
635 if (p->new == NULL && sb.st_size != copypos) {
636 h = STAILQ_FIRST(&p->head);
637 h->err = got_error(GOT_ERR_HUNK_FAILED);
638 err = h->err;
639 } else if (!feof(orig))
640 err = copy(tmp, orig, copypos, -1);
642 return err;
645 static const struct got_error *
646 report_progress(struct patch_args *pa, const char *old, const char *new,
647 unsigned char status, const struct got_error *orig_error)
649 const struct got_error *err;
650 struct got_patch_hunk *h;
652 err = pa->progress_cb(pa->progress_arg, old, new, status,
653 orig_error, 0, 0, 0, 0, 0, 0, NULL);
654 if (err)
655 return err;
657 STAILQ_FOREACH(h, pa->head, entries) {
658 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
659 continue;
661 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
662 h->old_from, h->old_lines, h->new_from, h->new_lines,
663 h->offset, h->ws_mangled, h->err);
664 if (err)
665 return err;
668 return NULL;
671 static const struct got_error *
672 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
673 const char *path)
675 return report_progress(arg, path, NULL, status, NULL);
678 static const struct got_error *
679 patch_add(void *arg, unsigned char status, const char *path)
681 return report_progress(arg, NULL, path, status, NULL);
684 static const struct got_error *
685 open_blob(char **path, FILE **fp, const char *blobid,
686 struct got_repository *repo)
688 const struct got_error *err = NULL;
689 struct got_blob_object *blob = NULL;
690 struct got_object_id id, *idptr, *matched_id = NULL;
691 int fd = -1;
693 *fp = NULL;
694 *path = NULL;
696 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
697 err = got_repo_match_object_id(&matched_id, NULL, blobid,
698 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
699 repo);
700 if (err)
701 return err;
702 idptr = matched_id;
703 } else {
704 if (!got_parse_sha1_digest(id.sha1, blobid))
705 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
706 idptr = &id;
709 fd = got_opentempfd();
710 if (fd == -1) {
711 err = got_error_from_errno("got_opentempfd");
712 goto done;
715 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
716 if (err)
717 goto done;
719 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
720 "");
721 if (err)
722 goto done;
724 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
725 if (err)
726 goto done;
728 done:
729 if (fd != -1 && close(fd) == -1 && err == NULL)
730 err = got_error_from_errno("close");
731 if (blob)
732 got_object_blob_close(blob);
733 if (matched_id != NULL)
734 free(matched_id);
735 if (err) {
736 if (*fp != NULL)
737 fclose(*fp);
738 if (*path != NULL)
739 unlink(*path);
740 free(*path);
741 *fp = NULL;
742 *path = NULL;
744 return err;
747 static const struct got_error *
748 prepare_merge(int *do_merge, char **apath, FILE **afile,
749 struct got_worktree *worktree, struct got_repository *repo,
750 struct got_patch *p, struct got_object_id *commit_id,
751 struct got_tree_object *tree, const char *path)
753 const struct got_error *err = NULL;
755 *do_merge = 0;
756 *apath = NULL;
757 *afile = NULL;
759 /* don't run the diff3 merge on creations/deletions */
760 if (p->old == NULL || p->new == NULL)
761 return NULL;
763 if (commit_id) {
764 struct got_object_id *id;
766 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
767 if (err)
768 return err;
769 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
770 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
771 free(id);
772 err = open_blob(apath, afile, p->blob, repo);
773 *do_merge = err == NULL;
774 } else if (*p->blob != '\0') {
775 err = open_blob(apath, afile, p->blob, repo);
776 /*
777 * ignore failures to open this blob, we might have
778 * parsed gibberish.
779 */
780 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
781 err->code != GOT_ERR_NO_OBJ)
782 return err;
783 *do_merge = err == NULL;
784 err = NULL;
787 return err;
790 static const struct got_error *
791 apply_patch(int *overlapcnt, struct got_worktree *worktree,
792 struct got_repository *repo, struct got_fileindex *fileindex,
793 const char *old, const char *new, struct got_patch *p, int nop,
794 int reverse, struct got_object_id *commit_id,
795 struct got_tree_object *tree, struct patch_args *pa,
796 got_cancel_cb cancel_cb, void *cancel_arg)
798 const struct got_error *err = NULL;
799 struct stat sb;
800 int do_merge = 0, file_renamed = 0;
801 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
802 char *oldpath = NULL, *newpath = NULL;
803 char *tmppath = NULL, *template = NULL;
804 char *apath = NULL, *mergepath = NULL;
805 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
806 int outfd;
807 mode_t mode = GOT_DEFAULT_FILE_MODE;
809 *overlapcnt = 0;
811 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
812 commit_id, tree, old);
813 if (err)
814 return err;
816 if (reverse && !do_merge)
817 reverse_patch(p);
819 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
820 old) == -1) {
821 err = got_error_from_errno("asprintf");
822 goto done;
825 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
826 new) == -1) {
827 err = got_error_from_errno("asprintf");
828 goto done;
831 file_renamed = strcmp(oldpath, newpath);
833 if (asprintf(&template, "%s/got-patch",
834 got_worktree_get_root_path(worktree)) == -1) {
835 err = got_error_from_errno(template);
836 goto done;
839 if (p->old != NULL) {
840 if ((oldfile = fopen(oldpath, "r")) == NULL) {
841 err = got_error_from_errno2("open", oldpath);
842 goto done;
844 if (fstat(fileno(oldfile), &sb) == -1) {
845 err = got_error_from_errno2("fstat", oldpath);
846 goto done;
848 mode = sb.st_mode;
849 } else if (p->xbit)
850 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
852 err = got_opentemp_named(&tmppath, &tmpfile, template, "");
853 if (err)
854 goto done;
855 outfd = fileno(tmpfile);
856 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
857 if (err)
858 goto done;
860 if (do_merge) {
861 const char *type, *id;
863 if (fseeko(afile, 0, SEEK_SET) == -1 ||
864 fseeko(oldfile, 0, SEEK_SET) == -1 ||
865 fseeko(tmpfile, 0, SEEK_SET) == -1) {
866 err = got_error_from_errno("fseeko");
867 goto done;
870 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
871 err = got_error_from_errno("asprintf");
872 oldlabel = NULL;
873 goto done;
876 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
877 err = got_error_from_errno("asprintf");
878 newlabel = NULL;
879 goto done;
882 if (*p->cid != '\0') {
883 type = "commit";
884 id = p->cid;
885 } else {
886 type = "blob";
887 id = p->blob;
890 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
891 err = got_error_from_errno("asprintf");
892 anclabel = NULL;
893 goto done;
896 if (reverse) {
897 char *s;
898 FILE *t;
900 s = anclabel;
901 anclabel = newlabel;
902 newlabel = s;
904 t = afile;
905 afile = tmpfile;
906 tmpfile = t;
909 err = got_opentemp_named(&mergepath, &mergefile, template, "");
910 if (err)
911 goto done;
912 outfd = fileno(mergefile);
914 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
915 oldfile, oldlabel, anclabel, newlabel,
916 GOT_DIFF_ALGORITHM_PATIENCE);
917 if (err)
918 goto done;
921 if (nop)
922 goto done;
924 if (p->old != NULL && p->new == NULL) {
925 err = got_worktree_patch_schedule_rm(old, repo, worktree,
926 fileindex, patch_delete, pa);
927 goto done;
930 if (fchmod(outfd, apply_umask(mode)) == -1) {
931 err = got_error_from_errno2("chmod", tmppath);
932 goto done;
935 if (mergepath) {
936 err = got_path_move_file(mergepath, newpath);
937 if (err)
938 goto done;
939 free(mergepath);
940 mergepath = NULL;
941 } else {
942 err = got_path_move_file(tmppath, newpath);
943 if (err)
944 goto done;
945 free(tmppath);
946 tmppath = NULL;
949 if (file_renamed) {
950 err = got_worktree_patch_schedule_rm(old, repo, worktree,
951 fileindex, patch_delete, pa);
952 if (err == NULL)
953 err = got_worktree_patch_schedule_add(new, repo,
954 worktree, fileindex, patch_add,
955 pa);
956 if (err)
957 unlink(newpath);
958 } else if (p->old == NULL) {
959 err = got_worktree_patch_schedule_add(new, repo, worktree,
960 fileindex, patch_add, pa);
961 if (err)
962 unlink(newpath);
963 } else if (*overlapcnt != 0)
964 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
965 else if (do_merge)
966 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
967 else
968 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
970 done:
971 free(template);
973 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
974 err = got_error_from_errno("unlink");
975 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
976 err = got_error_from_errno("fclose");
977 free(tmppath);
979 free(oldpath);
980 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
981 err = got_error_from_errno("fclose");
983 if (apath != NULL && unlink(apath) == -1 && err == NULL)
984 err = got_error_from_errno("unlink");
985 if (afile != NULL && fclose(afile) == EOF && err == NULL)
986 err = got_error_from_errno("fclose");
987 free(apath);
989 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
990 err = got_error_from_errno("unlink");
991 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
992 err = got_error_from_errno("fclose");
993 free(mergepath);
995 free(newpath);
996 free(oldlabel);
997 free(newlabel);
998 free(anclabel);
999 return err;
1002 const struct got_error *
1003 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1004 int nop, int strip, int reverse, struct got_object_id *commit_id,
1005 got_patch_progress_cb progress_cb, void *progress_arg,
1006 got_cancel_cb cancel_cb, void *cancel_arg)
1008 const struct got_error *err = NULL, *complete_err = NULL;
1009 struct got_fileindex *fileindex = NULL;
1010 struct got_commit_object *commit = NULL;
1011 struct got_tree_object *tree = NULL;
1012 char *fileindex_path = NULL;
1013 char *oldpath, *newpath;
1014 struct imsgbuf *ibuf;
1015 int imsg_fds[2] = {-1, -1};
1016 int overlapcnt, done = 0, failed = 0;
1017 pid_t pid;
1019 ibuf = calloc(1, sizeof(*ibuf));
1020 if (ibuf == NULL) {
1021 err = got_error_from_errno("calloc");
1022 goto done;
1025 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1026 err = got_error_from_errno("socketpair");
1027 goto done;
1030 pid = fork();
1031 if (pid == -1) {
1032 err = got_error_from_errno("fork");
1033 goto done;
1034 } else if (pid == 0) {
1035 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1036 NULL);
1037 /* not reached */
1040 if (close(imsg_fds[1]) == -1) {
1041 err = got_error_from_errno("close");
1042 goto done;
1044 imsg_fds[1] = -1;
1045 imsg_init(ibuf, imsg_fds[0]);
1047 err = send_patch(ibuf, fd);
1048 fd = -1;
1049 if (err)
1050 goto done;
1052 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1053 worktree);
1054 if (err)
1055 goto done;
1057 if (commit_id) {
1058 err = got_object_open_as_commit(&commit, repo, commit_id);
1059 if (err)
1060 goto done;
1062 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1063 if (err)
1064 goto done;
1067 while (!done && err == NULL) {
1068 struct got_patch p;
1069 struct patch_args pa;
1071 pa.progress_cb = progress_cb;
1072 pa.progress_arg = progress_arg;
1073 pa.head = &p.head;
1075 err = recv_patch(ibuf, &done, &p, strip);
1076 if (err || done)
1077 break;
1079 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1080 &newpath, worktree, repo, fileindex);
1081 if (err == NULL)
1082 err = apply_patch(&overlapcnt, worktree, repo,
1083 fileindex, oldpath, newpath, &p, nop, reverse,
1084 commit_id, tree, &pa, cancel_cb, cancel_arg);
1085 if (err != NULL) {
1086 failed = 1;
1087 /* recoverable errors */
1088 if (err->code == GOT_ERR_FILE_STATUS ||
1089 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1090 err = report_progress(&pa, p.old, p.new,
1091 GOT_STATUS_CANNOT_UPDATE, err);
1092 else if (err->code == GOT_ERR_HUNK_FAILED)
1093 err = report_progress(&pa, p.old, p.new,
1094 GOT_STATUS_CANNOT_UPDATE, NULL);
1096 if (overlapcnt != 0)
1097 failed = 1;
1099 free(oldpath);
1100 free(newpath);
1101 patch_free(&p);
1103 if (err)
1104 break;
1107 done:
1108 if (fileindex != NULL)
1109 complete_err = got_worktree_patch_complete(fileindex,
1110 fileindex_path);
1111 if (complete_err && err == NULL)
1112 err = complete_err;
1113 free(fileindex_path);
1114 if (tree)
1115 got_object_tree_close(tree);
1116 if (commit)
1117 got_object_commit_close(commit);
1118 if (fd != -1 && close(fd) == -1 && err == NULL)
1119 err = got_error_from_errno("close");
1120 if (ibuf != NULL)
1121 imsg_clear(ibuf);
1122 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1123 err = got_error_from_errno("close");
1124 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1125 err = got_error_from_errno("close");
1126 if (err == NULL && failed)
1127 err = got_error(GOT_ERR_PATCH_FAILED);
1128 return err;