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 /* When deleting binary files there are no hunks to apply. */
591 if (p->new == NULL && STAILQ_EMPTY(&p->head))
592 return NULL;
594 if (fstat(fileno(orig), &sb) == -1)
595 return got_error_from_errno("fstat");
597 copypos = 0;
598 STAILQ_FOREACH(h, &p->head, entries) {
599 tryagain:
600 err = locate_hunk(orig, h, &pos, &lineno);
601 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
602 h->err = err;
603 if (err != NULL)
604 return err;
605 err = copy(tmp, orig, copypos, pos);
606 if (err != NULL)
607 return err;
608 copypos = pos;
610 err = test_hunk(orig, h);
611 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
612 /*
613 * try to apply the hunk again starting the search
614 * after the previous partial match.
615 */
616 if (fseeko(orig, pos, SEEK_SET) == -1)
617 return got_error_from_errno("fseeko");
618 linelen = getline(&line, &linesize, orig);
619 if (linelen == -1)
620 return got_error_from_errno("getline");
621 lineno++;
622 goto tryagain;
624 if (err != NULL)
625 return err;
627 if (lineno + 1 != h->old_from)
628 h->offset = lineno + 1 - h->old_from;
630 err = apply_hunk(orig, tmp, h, &lineno, pos);
631 if (err != NULL)
632 return err;
634 copypos = ftello(orig);
635 if (copypos == -1)
636 return got_error_from_errno("ftello");
639 if (p->new == NULL && sb.st_size != copypos) {
640 h = STAILQ_FIRST(&p->head);
641 h->err = got_error(GOT_ERR_HUNK_FAILED);
642 err = h->err;
643 } else if (!feof(orig))
644 err = copy(tmp, orig, copypos, -1);
646 return err;
649 static const struct got_error *
650 report_progress(struct patch_args *pa, const char *old, const char *new,
651 unsigned char status, const struct got_error *orig_error)
653 const struct got_error *err;
654 struct got_patch_hunk *h;
656 err = pa->progress_cb(pa->progress_arg, old, new, status,
657 orig_error, 0, 0, 0, 0, 0, 0, NULL);
658 if (err)
659 return err;
661 STAILQ_FOREACH(h, pa->head, entries) {
662 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
663 continue;
665 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
666 h->old_from, h->old_lines, h->new_from, h->new_lines,
667 h->offset, h->ws_mangled, h->err);
668 if (err)
669 return err;
672 return NULL;
675 static const struct got_error *
676 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
677 const char *path)
679 return report_progress(arg, path, NULL, status, NULL);
682 static const struct got_error *
683 patch_add(void *arg, unsigned char status, const char *path)
685 return report_progress(arg, NULL, path, status, NULL);
688 static const struct got_error *
689 open_blob(char **path, FILE **fp, const char *blobid,
690 struct got_repository *repo)
692 const struct got_error *err = NULL;
693 struct got_blob_object *blob = NULL;
694 struct got_object_id id, *idptr, *matched_id = NULL;
695 int fd = -1;
697 *fp = NULL;
698 *path = NULL;
700 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
701 err = got_repo_match_object_id(&matched_id, NULL, blobid,
702 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
703 repo);
704 if (err)
705 return err;
706 idptr = matched_id;
707 } else {
708 if (!got_parse_sha1_digest(id.sha1, blobid))
709 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
710 idptr = &id;
713 fd = got_opentempfd();
714 if (fd == -1) {
715 err = got_error_from_errno("got_opentempfd");
716 goto done;
719 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
720 if (err)
721 goto done;
723 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
724 "");
725 if (err)
726 goto done;
728 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
729 if (err)
730 goto done;
732 done:
733 if (fd != -1 && close(fd) == -1 && err == NULL)
734 err = got_error_from_errno("close");
735 if (blob)
736 got_object_blob_close(blob);
737 if (matched_id != NULL)
738 free(matched_id);
739 if (err) {
740 if (*fp != NULL)
741 fclose(*fp);
742 if (*path != NULL)
743 unlink(*path);
744 free(*path);
745 *fp = NULL;
746 *path = NULL;
748 return err;
751 static const struct got_error *
752 prepare_merge(int *do_merge, char **apath, FILE **afile,
753 struct got_worktree *worktree, struct got_repository *repo,
754 struct got_patch *p, struct got_object_id *commit_id,
755 struct got_tree_object *tree, const char *path)
757 const struct got_error *err = NULL;
759 *do_merge = 0;
760 *apath = NULL;
761 *afile = NULL;
763 /* don't run the diff3 merge on creations/deletions */
764 if (p->old == NULL || p->new == NULL)
765 return NULL;
767 if (commit_id) {
768 struct got_object_id *id;
770 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
771 if (err)
772 return err;
773 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
774 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
775 free(id);
776 err = open_blob(apath, afile, p->blob, repo);
777 *do_merge = err == NULL;
778 } else if (*p->blob != '\0') {
779 err = open_blob(apath, afile, p->blob, repo);
780 /*
781 * ignore failures to open this blob, we might have
782 * parsed gibberish.
783 */
784 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
785 err->code != GOT_ERR_NO_OBJ)
786 return err;
787 *do_merge = err == NULL;
788 err = NULL;
791 return err;
794 static const struct got_error *
795 apply_patch(int *overlapcnt, struct got_worktree *worktree,
796 struct got_repository *repo, struct got_fileindex *fileindex,
797 const char *old, const char *new, struct got_patch *p, int nop,
798 int reverse, struct got_object_id *commit_id,
799 struct got_tree_object *tree, struct patch_args *pa,
800 got_cancel_cb cancel_cb, void *cancel_arg)
802 const struct got_error *err = NULL;
803 struct stat sb;
804 int do_merge = 0, file_renamed = 0;
805 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
806 char *oldpath = NULL, *newpath = NULL;
807 char *tmppath = NULL, *template = NULL;
808 char *apath = NULL, *mergepath = NULL;
809 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
810 int outfd;
811 mode_t mode = GOT_DEFAULT_FILE_MODE;
813 *overlapcnt = 0;
815 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
816 commit_id, tree, old);
817 if (err)
818 return err;
820 if (reverse && !do_merge)
821 reverse_patch(p);
823 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
824 old) == -1) {
825 err = got_error_from_errno("asprintf");
826 goto done;
829 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
830 new) == -1) {
831 err = got_error_from_errno("asprintf");
832 goto done;
835 file_renamed = strcmp(oldpath, newpath);
837 if (asprintf(&template, "%s/got-patch",
838 got_worktree_get_root_path(worktree)) == -1) {
839 err = got_error_from_errno(template);
840 goto done;
843 if (p->old != NULL) {
844 if ((oldfile = fopen(oldpath, "r")) == NULL) {
845 err = got_error_from_errno2("open", oldpath);
846 goto done;
848 if (fstat(fileno(oldfile), &sb) == -1) {
849 err = got_error_from_errno2("fstat", oldpath);
850 goto done;
852 mode = sb.st_mode;
853 } else if (p->xbit)
854 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
856 err = got_opentemp_named(&tmppath, &tmpfile, template, "");
857 if (err)
858 goto done;
859 outfd = fileno(tmpfile);
860 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
861 if (err)
862 goto done;
864 if (do_merge) {
865 const char *type, *id;
867 if (fseeko(afile, 0, SEEK_SET) == -1 ||
868 fseeko(oldfile, 0, SEEK_SET) == -1 ||
869 fseeko(tmpfile, 0, SEEK_SET) == -1) {
870 err = got_error_from_errno("fseeko");
871 goto done;
874 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
875 err = got_error_from_errno("asprintf");
876 oldlabel = NULL;
877 goto done;
880 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
881 err = got_error_from_errno("asprintf");
882 newlabel = NULL;
883 goto done;
886 if (*p->cid != '\0') {
887 type = "commit";
888 id = p->cid;
889 } else {
890 type = "blob";
891 id = p->blob;
894 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
895 err = got_error_from_errno("asprintf");
896 anclabel = NULL;
897 goto done;
900 if (reverse) {
901 char *s;
902 FILE *t;
904 s = anclabel;
905 anclabel = newlabel;
906 newlabel = s;
908 t = afile;
909 afile = tmpfile;
910 tmpfile = t;
913 err = got_opentemp_named(&mergepath, &mergefile, template, "");
914 if (err)
915 goto done;
916 outfd = fileno(mergefile);
918 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
919 oldfile, oldlabel, anclabel, newlabel,
920 GOT_DIFF_ALGORITHM_PATIENCE);
921 if (err)
922 goto done;
925 if (nop)
926 goto done;
928 if (p->old != NULL && p->new == NULL) {
929 err = got_worktree_patch_schedule_rm(old, repo, worktree,
930 fileindex, patch_delete, pa);
931 goto done;
934 if (fchmod(outfd, apply_umask(mode)) == -1) {
935 err = got_error_from_errno2("chmod", tmppath);
936 goto done;
939 if (mergepath) {
940 err = got_path_move_file(mergepath, newpath);
941 if (err)
942 goto done;
943 free(mergepath);
944 mergepath = NULL;
945 } else {
946 err = got_path_move_file(tmppath, newpath);
947 if (err)
948 goto done;
949 free(tmppath);
950 tmppath = NULL;
953 if (file_renamed) {
954 err = got_worktree_patch_schedule_rm(old, repo, worktree,
955 fileindex, patch_delete, pa);
956 if (err == NULL)
957 err = got_worktree_patch_schedule_add(new, repo,
958 worktree, fileindex, patch_add,
959 pa);
960 if (err)
961 unlink(newpath);
962 } else if (p->old == NULL) {
963 err = got_worktree_patch_schedule_add(new, repo, worktree,
964 fileindex, patch_add, pa);
965 if (err)
966 unlink(newpath);
967 } else if (*overlapcnt != 0)
968 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
969 else if (do_merge)
970 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
971 else
972 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
974 done:
975 free(template);
977 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
978 err = got_error_from_errno("unlink");
979 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
980 err = got_error_from_errno("fclose");
981 free(tmppath);
983 free(oldpath);
984 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
985 err = got_error_from_errno("fclose");
987 if (apath != NULL && unlink(apath) == -1 && err == NULL)
988 err = got_error_from_errno("unlink");
989 if (afile != NULL && fclose(afile) == EOF && err == NULL)
990 err = got_error_from_errno("fclose");
991 free(apath);
993 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
994 err = got_error_from_errno("unlink");
995 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
996 err = got_error_from_errno("fclose");
997 free(mergepath);
999 free(newpath);
1000 free(oldlabel);
1001 free(newlabel);
1002 free(anclabel);
1003 return err;
1006 const struct got_error *
1007 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1008 int nop, int strip, int reverse, struct got_object_id *commit_id,
1009 got_patch_progress_cb progress_cb, void *progress_arg,
1010 got_cancel_cb cancel_cb, void *cancel_arg)
1012 const struct got_error *err = NULL, *complete_err = NULL;
1013 struct got_fileindex *fileindex = NULL;
1014 struct got_commit_object *commit = NULL;
1015 struct got_tree_object *tree = NULL;
1016 char *fileindex_path = NULL;
1017 char *oldpath, *newpath;
1018 struct imsgbuf *ibuf;
1019 int imsg_fds[2] = {-1, -1};
1020 int overlapcnt, done = 0, failed = 0;
1021 pid_t pid;
1023 ibuf = calloc(1, sizeof(*ibuf));
1024 if (ibuf == NULL) {
1025 err = got_error_from_errno("calloc");
1026 goto done;
1029 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1030 err = got_error_from_errno("socketpair");
1031 goto done;
1034 pid = fork();
1035 if (pid == -1) {
1036 err = got_error_from_errno("fork");
1037 goto done;
1038 } else if (pid == 0) {
1039 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1040 NULL);
1041 /* not reached */
1044 if (close(imsg_fds[1]) == -1) {
1045 err = got_error_from_errno("close");
1046 goto done;
1048 imsg_fds[1] = -1;
1049 imsg_init(ibuf, imsg_fds[0]);
1051 err = send_patch(ibuf, fd);
1052 fd = -1;
1053 if (err)
1054 goto done;
1056 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1057 worktree);
1058 if (err)
1059 goto done;
1061 if (commit_id) {
1062 err = got_object_open_as_commit(&commit, repo, commit_id);
1063 if (err)
1064 goto done;
1066 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1067 if (err)
1068 goto done;
1071 while (!done && err == NULL) {
1072 struct got_patch p;
1073 struct patch_args pa;
1075 pa.progress_cb = progress_cb;
1076 pa.progress_arg = progress_arg;
1077 pa.head = &p.head;
1079 err = recv_patch(ibuf, &done, &p, strip);
1080 if (err || done)
1081 break;
1083 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1084 &newpath, worktree, repo, fileindex);
1085 if (err == NULL)
1086 err = apply_patch(&overlapcnt, worktree, repo,
1087 fileindex, oldpath, newpath, &p, nop, reverse,
1088 commit_id, tree, &pa, cancel_cb, cancel_arg);
1089 if (err != NULL) {
1090 failed = 1;
1091 /* recoverable errors */
1092 if (err->code == GOT_ERR_FILE_STATUS ||
1093 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1094 err = report_progress(&pa, p.old, p.new,
1095 GOT_STATUS_CANNOT_UPDATE, err);
1096 else if (err->code == GOT_ERR_HUNK_FAILED)
1097 err = report_progress(&pa, p.old, p.new,
1098 GOT_STATUS_CANNOT_UPDATE, NULL);
1100 if (overlapcnt != 0)
1101 failed = 1;
1103 free(oldpath);
1104 free(newpath);
1105 patch_free(&p);
1107 if (err)
1108 break;
1111 done:
1112 if (fileindex != NULL)
1113 complete_err = got_worktree_patch_complete(fileindex,
1114 fileindex_path);
1115 if (complete_err && err == NULL)
1116 err = complete_err;
1117 free(fileindex_path);
1118 if (tree)
1119 got_object_tree_close(tree);
1120 if (commit)
1121 got_object_commit_close(commit);
1122 if (fd != -1 && close(fd) == -1 && err == NULL)
1123 err = got_error_from_errno("close");
1124 if (ibuf != NULL)
1125 imsg_clear(ibuf);
1126 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1127 err = got_error_from_errno("close");
1128 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1129 err = got_error_from_errno("close");
1130 if (err == NULL && failed)
1131 err = got_error(GOT_ERR_PATCH_FAILED);
1132 return err;