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 <sha2.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_path.h"
42 #include "got_reference.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_repository.h"
46 #include "got_opentemp.h"
47 #include "got_patch.h"
48 #include "got_diff.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_diff.h"
52 #include "got_lib_object.h"
53 #include "got_lib_privsep.h"
54 #include "got_lib_hash.h"
56 #define MIN(a, b) ((a) < (b) ? (a) : (b))
58 struct got_patch_hunk {
59 STAILQ_ENTRY(got_patch_hunk) entries;
60 const struct got_error *err;
61 int ws_mangled;
62 int offset;
63 int old_nonl;
64 int new_nonl;
65 int old_from;
66 int old_lines;
67 int new_from;
68 int new_lines;
69 size_t len;
70 size_t cap;
71 char **lines;
72 };
74 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
75 struct got_patch {
76 int xbit;
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 mode_t
91 apply_umask(mode_t mode)
92 {
93 mode_t um;
95 um = umask(000);
96 umask(um);
97 return mode & ~um;
98 }
100 static const struct got_error *
101 send_patch(struct imsgbuf *ibuf, int fd)
103 const struct got_error *err = NULL;
105 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
106 NULL, 0) == -1) {
107 err = got_error_from_errno(
108 "imsg_compose GOT_IMSG_PATCH_FILE");
109 close(fd);
110 return err;
113 return got_privsep_flush_imsg(ibuf);
116 static void
117 patch_free(struct got_patch *p)
119 struct got_patch_hunk *h;
120 size_t i;
122 while (!STAILQ_EMPTY(&p->head)) {
123 h = STAILQ_FIRST(&p->head);
124 STAILQ_REMOVE_HEAD(&p->head, entries);
126 for (i = 0; i < h->len; ++i)
127 free(h->lines[i]);
128 free(h->lines);
129 free(h);
132 free(p->new);
133 free(p->old);
135 memset(p, 0, sizeof(*p));
136 STAILQ_INIT(&p->head);
139 static const struct got_error *
140 pushline(struct got_patch_hunk *h, const char *line)
142 void *t;
143 size_t newcap;
145 if (h->len == h->cap) {
146 if ((newcap = h->cap * 1.5) == 0)
147 newcap = 16;
148 t = recallocarray(h->lines, h->cap, newcap,
149 sizeof(h->lines[0]));
150 if (t == NULL)
151 return got_error_from_errno("recallocarray");
152 h->lines = t;
153 h->cap = newcap;
156 if ((t = strdup(line)) == NULL)
157 return got_error_from_errno("strdup");
159 h->lines[h->len++] = t;
160 return NULL;
163 static const struct got_error *
164 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
166 const struct got_error *err = NULL;
167 struct imsg imsg;
168 struct got_imsg_patch_hunk hdr;
169 struct got_imsg_patch patch;
170 struct got_patch_hunk *h = NULL;
171 size_t datalen;
172 int lastmode = -1;
174 memset(p, 0, sizeof(*p));
175 STAILQ_INIT(&p->head);
177 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
178 if (err)
179 return err;
180 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
181 *done = 1;
182 goto done;
184 if (imsg.hdr.type != GOT_IMSG_PATCH) {
185 err = got_error(GOT_ERR_PRIVSEP_MSG);
186 goto done;
188 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
189 if (datalen != sizeof(patch)) {
190 err = got_error(GOT_ERR_PRIVSEP_LEN);
191 goto done;
193 memcpy(&patch, imsg.data, sizeof(patch));
195 if (patch.old[sizeof(patch.old)-1] != '\0' ||
196 patch.new[sizeof(patch.new)-1] != '\0' ||
197 patch.cid[sizeof(patch.cid)-1] != '\0' ||
198 patch.blob[sizeof(patch.blob)-1] != '\0') {
199 err = got_error(GOT_ERR_PRIVSEP_LEN);
200 goto done;
203 if (*patch.cid != '\0')
204 strlcpy(p->cid, patch.cid, sizeof(p->cid));
206 if (*patch.blob != '\0')
207 strlcpy(p->blob, patch.blob, sizeof(p->blob));
209 p->xbit = patch.xbit;
211 /* automatically set strip=1 for git-style diffs */
212 if (strip == -1 && patch.git &&
213 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
214 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
215 strip = 1;
217 /* prefer the new name if not /dev/null for not git-style diffs */
218 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
219 err = got_path_strip(&p->old, patch.new, strip);
220 if (err)
221 goto done;
222 } else if (*patch.old != '\0') {
223 err = got_path_strip(&p->old, patch.old, strip);
224 if (err)
225 goto done;
228 if (*patch.new != '\0') {
229 err = got_path_strip(&p->new, patch.new, strip);
230 if (err)
231 goto done;
234 if (p->old == NULL && p->new == NULL) {
235 err = got_error(GOT_ERR_PATCH_MALFORMED);
236 goto done;
239 imsg_free(&imsg);
241 for (;;) {
242 char *t;
244 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
245 if (err) {
246 patch_free(p);
247 return err;
250 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
251 switch (imsg.hdr.type) {
252 case GOT_IMSG_PATCH_DONE:
253 if (h != NULL && h->len == 0)
254 err = got_error(GOT_ERR_PATCH_MALFORMED);
255 goto done;
256 case GOT_IMSG_PATCH_HUNK:
257 if (h != NULL &&
258 (h->len == 0 || h->old_nonl || h->new_nonl)) {
259 err = got_error(GOT_ERR_PATCH_MALFORMED);
260 goto done;
262 lastmode = -1;
263 if (datalen != sizeof(hdr)) {
264 err = got_error(GOT_ERR_PRIVSEP_LEN);
265 goto done;
267 memcpy(&hdr, imsg.data, sizeof(hdr));
268 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
269 err = got_error(GOT_ERR_PRIVSEP_LEN);
270 goto done;
272 if ((h = calloc(1, sizeof(*h))) == NULL) {
273 err = got_error_from_errno("calloc");
274 goto done;
276 h->old_from = hdr.oldfrom;
277 h->old_lines = hdr.oldlines;
278 h->new_from = hdr.newfrom;
279 h->new_lines = hdr.newlines;
280 STAILQ_INSERT_TAIL(&p->head, h, entries);
281 break;
282 case GOT_IMSG_PATCH_LINE:
283 if (h == NULL) {
284 err = got_error(GOT_ERR_PRIVSEP_MSG);
285 goto done;
287 t = imsg.data;
288 /* at least one char */
289 if (datalen < 2 || t[datalen-1] != '\0') {
290 err = got_error(GOT_ERR_PRIVSEP_MSG);
291 goto done;
293 if (*t != ' ' && *t != '-' && *t != '+' &&
294 *t != '\\') {
295 err = got_error(GOT_ERR_PRIVSEP_MSG);
296 goto done;
299 if (*t != '\\')
300 err = pushline(h, t);
301 else if (lastmode == '-')
302 h->old_nonl = 1;
303 else if (lastmode == '+')
304 h->new_nonl = 1;
305 else
306 err = got_error(GOT_ERR_PATCH_MALFORMED);
308 if (err)
309 goto done;
311 lastmode = *t;
312 break;
313 default:
314 err = got_error(GOT_ERR_PRIVSEP_MSG);
315 goto done;
318 imsg_free(&imsg);
321 done:
322 if (err)
323 patch_free(p);
325 imsg_free(&imsg);
326 return err;
329 static void
330 reverse_patch(struct got_patch *p)
332 struct got_patch_hunk *h;
333 size_t i;
334 int tmp;
336 STAILQ_FOREACH(h, &p->head, entries) {
337 tmp = h->old_from;
338 h->old_from = h->new_from;
339 h->new_from = tmp;
341 tmp = h->old_lines;
342 h->old_lines = h->new_lines;
343 h->new_lines = tmp;
345 tmp = h->old_nonl;
346 h->old_nonl = h->new_nonl;
347 h->new_nonl = tmp;
349 for (i = 0; i < h->len; ++i) {
350 if (*h->lines[i] == '+')
351 *h->lines[i] = '-';
352 else if (*h->lines[i] == '-')
353 *h->lines[i] = '+';
358 /*
359 * Copy data from orig starting at copypos until pos into tmp.
360 * If pos is -1, copy until EOF.
361 */
362 static const struct got_error *
363 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
365 char buf[BUFSIZ];
366 size_t len, r, w;
368 if (fseeko(orig, copypos, SEEK_SET) == -1)
369 return got_error_from_errno("fseeko");
371 while (pos == -1 || copypos < pos) {
372 len = sizeof(buf);
373 if (pos > 0)
374 len = MIN(len, (size_t)pos - copypos);
375 r = fread(buf, 1, len, orig);
376 if (r != len && ferror(orig))
377 return got_error_from_errno("fread");
378 w = fwrite(buf, 1, r, tmp);
379 if (w != r)
380 return got_error_from_errno("fwrite");
381 copypos += len;
382 if (r != len && feof(orig)) {
383 if (pos == -1)
384 return NULL;
385 return got_error(GOT_ERR_HUNK_FAILED);
388 return NULL;
391 static int linecmp(const char *, const char *, int *);
393 static const struct got_error *
394 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
396 const struct got_error *err = NULL;
397 char *line = NULL;
398 char mode = *h->lines[0];
399 size_t linesize = 0;
400 ssize_t linelen;
401 off_t match = -1;
402 int mangled = 0, match_lineno = -1;
404 for (;;) {
405 linelen = getline(&line, &linesize, orig);
406 if (linelen == -1) {
407 if (ferror(orig))
408 err = got_error_from_errno("getline");
409 else if (match == -1)
410 err = got_error(GOT_ERR_HUNK_FAILED);
411 break;
413 if (line[linelen - 1] == '\n')
414 line[linelen - 1] = '\0';
415 (*lineno)++;
417 if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
418 (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
419 (mode == '+' && *lineno == h->old_from)) {
420 match = ftello(orig);
421 if (match == -1) {
422 err = got_error_from_errno("ftello");
423 break;
425 match -= linelen;
426 match_lineno = (*lineno)-1;
429 if (*lineno >= h->old_from && match != -1) {
430 if (mangled)
431 h->ws_mangled = 1;
432 break;
436 if (err == NULL) {
437 *pos = match;
438 *lineno = match_lineno;
439 if (fseeko(orig, match, SEEK_SET) == -1)
440 err = got_error_from_errno("fseeko");
443 free(line);
444 return err;
447 static int
448 linecmp(const char *a, const char *b, int *mangled)
450 int c;
452 *mangled = 0;
453 c = strcmp(a, b);
454 if (c == 0)
455 return c;
457 *mangled = 1;
458 for (;;) {
459 while (*a == '\t' || *a == ' ' || *a == '\f')
460 a++;
461 while (*b == '\t' || *b == ' ' || *b == '\f')
462 b++;
463 if (*a == '\0' || *a != *b)
464 break;
465 a++, b++;
468 return *a - *b;
471 static const struct got_error *
472 test_hunk(FILE *orig, struct got_patch_hunk *h)
474 const struct got_error *err = NULL;
475 char *line = NULL;
476 size_t linesize = 0, i = 0;
477 ssize_t linelen;
478 int mangled;
480 for (i = 0; i < h->len; ++i) {
481 switch (*h->lines[i]) {
482 case '+':
483 continue;
484 case ' ':
485 case '-':
486 linelen = getline(&line, &linesize, orig);
487 if (linelen == -1) {
488 if (ferror(orig))
489 err = got_error_from_errno("getline");
490 else
491 err = got_error(
492 GOT_ERR_HUNK_FAILED);
493 goto done;
495 if (line[linelen - 1] == '\n')
496 line[linelen - 1] = '\0';
497 if (linecmp(h->lines[i] + 1, line, &mangled)) {
498 err = got_error(GOT_ERR_HUNK_FAILED);
499 goto done;
501 if (mangled)
502 h->ws_mangled = 1;
503 break;
507 done:
508 free(line);
509 return err;
512 static const struct got_error *
513 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
514 off_t from)
516 const struct got_error *err = NULL;
517 const char *t;
518 size_t linesize = 0, i, new = 0;
519 char *line = NULL;
520 char mode;
521 ssize_t linelen;
523 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
524 return got_error_from_errno("fseeko");
526 for (i = 0; i < h->len; ++i) {
527 switch (mode = *h->lines[i]) {
528 case '-':
529 case ' ':
530 (*lineno)++;
531 if (orig != NULL) {
532 linelen = getline(&line, &linesize, orig);
533 if (linelen == -1) {
534 err = got_error_from_errno("getline");
535 goto done;
537 if (line[linelen - 1] == '\n')
538 line[linelen - 1] = '\0';
539 t = line;
540 } else
541 t = h->lines[i] + 1;
542 if (mode == '-')
543 continue;
544 if (fprintf(tmp, "%s\n", t) < 0) {
545 err = got_error_from_errno("fprintf");
546 goto done;
548 break;
549 case '+':
550 new++;
551 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
552 err = got_error_from_errno("fprintf");
553 goto done;
555 if (new != h->new_lines || !h->new_nonl) {
556 if (fprintf(tmp, "\n") < 0) {
557 err = got_error_from_errno("fprintf");
558 goto done;
561 break;
565 done:
566 free(line);
567 return err;
570 static const struct got_error *
571 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
573 const struct got_error *err = NULL;
574 struct got_patch_hunk *h;
575 struct stat sb;
576 int lineno = 0;
577 off_t copypos, pos;
578 char *line = NULL;
579 size_t linesize = 0;
580 ssize_t linelen;
582 if (p->old == NULL) { /* create */
583 h = STAILQ_FIRST(&p->head);
584 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
585 return got_error(GOT_ERR_PATCH_MALFORMED);
586 return apply_hunk(orig, tmp, h, &lineno, 0);
589 /* When deleting binary files there are no hunks to apply. */
590 if (p->new == NULL && STAILQ_EMPTY(&p->head))
591 return NULL;
593 if (fstat(fileno(orig), &sb) == -1)
594 return got_error_from_errno("fstat");
596 copypos = 0;
597 STAILQ_FOREACH(h, &p->head, entries) {
598 tryagain:
599 err = locate_hunk(orig, h, &pos, &lineno);
600 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
601 h->err = err;
602 if (err != NULL)
603 return err;
604 err = copy(tmp, orig, copypos, pos);
605 if (err != NULL)
606 return err;
607 copypos = pos;
609 err = test_hunk(orig, h);
610 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
611 /*
612 * try to apply the hunk again starting the search
613 * after the previous partial match.
614 */
615 if (fseeko(orig, pos, SEEK_SET) == -1)
616 return got_error_from_errno("fseeko");
617 linelen = getline(&line, &linesize, orig);
618 if (linelen == -1)
619 return got_error_from_errno("getline");
620 lineno++;
621 goto tryagain;
623 if (err != NULL)
624 return err;
626 if (lineno + 1 != h->old_from)
627 h->offset = lineno + 1 - h->old_from;
629 err = apply_hunk(orig, tmp, h, &lineno, pos);
630 if (err != NULL)
631 return err;
633 copypos = ftello(orig);
634 if (copypos == -1)
635 return got_error_from_errno("ftello");
638 if (p->new == NULL && sb.st_size != copypos) {
639 h = STAILQ_FIRST(&p->head);
640 h->err = got_error(GOT_ERR_HUNK_FAILED);
641 err = h->err;
642 } else if (!feof(orig))
643 err = copy(tmp, orig, copypos, -1);
645 return err;
648 static const struct got_error *
649 report_progress(struct patch_args *pa, const char *old, const char *new,
650 unsigned char status, const struct got_error *orig_error)
652 const struct got_error *err;
653 struct got_patch_hunk *h;
655 err = pa->progress_cb(pa->progress_arg, old, new, status,
656 orig_error, 0, 0, 0, 0, 0, 0, NULL);
657 if (err)
658 return err;
660 STAILQ_FOREACH(h, pa->head, entries) {
661 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
662 continue;
664 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
665 h->old_from, h->old_lines, h->new_from, h->new_lines,
666 h->offset, h->ws_mangled, h->err);
667 if (err)
668 return err;
671 return NULL;
674 static const struct got_error *
675 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
676 const char *path)
678 return report_progress(arg, path, NULL, status, NULL);
681 static const struct got_error *
682 patch_add(void *arg, unsigned char status, const char *path)
684 return report_progress(arg, NULL, path, status, NULL);
687 static const struct got_error *
688 open_blob(char **path, FILE **fp, const char *blobid,
689 struct got_repository *repo)
691 const struct got_error *err = NULL;
692 struct got_blob_object *blob = NULL;
693 struct got_object_id id, *idptr, *matched_id = NULL;
694 int fd = -1;
696 *fp = NULL;
697 *path = NULL;
699 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
700 err = got_repo_match_object_id(&matched_id, NULL, blobid,
701 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
702 repo);
703 if (err)
704 return err;
705 idptr = matched_id;
706 } else {
707 if (!got_parse_object_id(&id, blobid, GOT_HASH_SHA1))
708 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
709 idptr = &id;
712 fd = got_opentempfd();
713 if (fd == -1) {
714 err = got_error_from_errno("got_opentempfd");
715 goto done;
718 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
719 if (err)
720 goto done;
722 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
723 "");
724 if (err)
725 goto done;
727 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
728 if (err)
729 goto done;
731 done:
732 if (fd != -1 && close(fd) == -1 && err == NULL)
733 err = got_error_from_errno("close");
734 if (blob)
735 got_object_blob_close(blob);
736 if (matched_id != NULL)
737 free(matched_id);
738 if (err) {
739 if (*fp != NULL)
740 fclose(*fp);
741 if (*path != NULL)
742 unlink(*path);
743 free(*path);
744 *fp = NULL;
745 *path = NULL;
747 return err;
750 static const struct got_error *
751 prepare_merge(int *do_merge, char **apath, FILE **afile,
752 struct got_worktree *worktree, struct got_repository *repo,
753 struct got_patch *p, struct got_object_id *commit_id,
754 struct got_tree_object *tree, const char *path)
756 const struct got_error *err = NULL;
758 *do_merge = 0;
759 *apath = NULL;
760 *afile = NULL;
762 /* don't run the diff3 merge on creations/deletions */
763 if (p->old == NULL || p->new == NULL)
764 return NULL;
766 if (commit_id) {
767 struct got_object_id *id;
769 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
770 if (err)
771 return err;
772 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
773 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
774 free(id);
775 err = open_blob(apath, afile, p->blob, repo);
776 *do_merge = err == NULL;
777 } else if (*p->blob != '\0') {
778 err = open_blob(apath, afile, p->blob, repo);
779 /*
780 * ignore failures to open this blob, we might have
781 * parsed gibberish.
782 */
783 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
784 err->code != GOT_ERR_NO_OBJ)
785 return err;
786 *do_merge = err == NULL;
787 err = NULL;
790 return err;
793 static const struct got_error *
794 apply_patch(int *overlapcnt, struct got_worktree *worktree,
795 struct got_repository *repo, struct got_fileindex *fileindex,
796 const char *old, const char *new, struct got_patch *p, int nop,
797 int reverse, struct got_object_id *commit_id,
798 struct got_tree_object *tree, struct patch_args *pa,
799 got_cancel_cb cancel_cb, void *cancel_arg)
801 const struct got_error *err = NULL;
802 struct stat sb;
803 int do_merge = 0, file_renamed = 0;
804 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
805 char *oldpath = NULL, *newpath = NULL;
806 char *tmppath = NULL, *template = NULL;
807 char *apath = NULL, *mergepath = NULL;
808 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
809 int outfd;
810 mode_t mode = GOT_DEFAULT_FILE_MODE;
812 *overlapcnt = 0;
814 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
815 commit_id, tree, old);
816 if (err)
817 return err;
819 if (reverse && !do_merge)
820 reverse_patch(p);
822 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
823 old) == -1) {
824 err = got_error_from_errno("asprintf");
825 goto done;
828 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
829 new) == -1) {
830 err = got_error_from_errno("asprintf");
831 goto done;
834 file_renamed = strcmp(oldpath, newpath);
836 if (asprintf(&template, "%s/got-patch",
837 got_worktree_get_root_path(worktree)) == -1) {
838 err = got_error_from_errno(template);
839 goto done;
842 if (p->old != NULL) {
843 if ((oldfile = fopen(oldpath, "r")) == NULL) {
844 err = got_error_from_errno2("open", oldpath);
845 goto done;
847 if (fstat(fileno(oldfile), &sb) == -1) {
848 err = got_error_from_errno2("fstat", oldpath);
849 goto done;
851 mode = sb.st_mode;
852 } else if (p->xbit)
853 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
855 err = got_opentemp_named(&tmppath, &tmpfile, template, "");
856 if (err)
857 goto done;
858 outfd = fileno(tmpfile);
859 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
860 if (err)
861 goto done;
863 if (do_merge) {
864 const char *type, *id;
866 if (fseeko(afile, 0, SEEK_SET) == -1 ||
867 fseeko(oldfile, 0, SEEK_SET) == -1 ||
868 fseeko(tmpfile, 0, SEEK_SET) == -1) {
869 err = got_error_from_errno("fseeko");
870 goto done;
873 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
874 err = got_error_from_errno("asprintf");
875 oldlabel = NULL;
876 goto done;
879 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
880 err = got_error_from_errno("asprintf");
881 newlabel = NULL;
882 goto done;
885 if (*p->cid != '\0') {
886 type = "commit";
887 id = p->cid;
888 } else {
889 type = "blob";
890 id = p->blob;
893 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
894 err = got_error_from_errno("asprintf");
895 anclabel = NULL;
896 goto done;
899 if (reverse) {
900 char *s;
901 FILE *t;
903 s = anclabel;
904 anclabel = newlabel;
905 newlabel = s;
907 t = afile;
908 afile = tmpfile;
909 tmpfile = t;
912 err = got_opentemp_named(&mergepath, &mergefile, template, "");
913 if (err)
914 goto done;
915 outfd = fileno(mergefile);
917 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
918 oldfile, oldlabel, anclabel, newlabel,
919 GOT_DIFF_ALGORITHM_PATIENCE);
920 if (err)
921 goto done;
924 if (nop)
925 goto done;
927 if (p->old != NULL && p->new == NULL) {
928 err = got_worktree_patch_schedule_rm(old, repo, worktree,
929 fileindex, patch_delete, pa);
930 goto done;
933 if (fchmod(outfd, apply_umask(mode)) == -1) {
934 err = got_error_from_errno2("chmod", tmppath);
935 goto done;
938 if (mergepath) {
939 err = got_path_move_file(mergepath, newpath);
940 if (err)
941 goto done;
942 free(mergepath);
943 mergepath = NULL;
944 } else {
945 err = got_path_move_file(tmppath, newpath);
946 if (err)
947 goto done;
948 free(tmppath);
949 tmppath = NULL;
952 if (file_renamed) {
953 err = got_worktree_patch_schedule_rm(old, repo, worktree,
954 fileindex, patch_delete, pa);
955 if (err == NULL)
956 err = got_worktree_patch_schedule_add(new, repo,
957 worktree, fileindex, patch_add,
958 pa);
959 if (err)
960 unlink(newpath);
961 } else if (p->old == NULL) {
962 err = got_worktree_patch_schedule_add(new, repo, worktree,
963 fileindex, patch_add, pa);
964 if (err)
965 unlink(newpath);
966 } else if (*overlapcnt != 0)
967 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
968 else if (do_merge)
969 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
970 else
971 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
973 done:
974 free(template);
976 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
977 err = got_error_from_errno("unlink");
978 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
979 err = got_error_from_errno("fclose");
980 free(tmppath);
982 free(oldpath);
983 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
984 err = got_error_from_errno("fclose");
986 if (apath != NULL && unlink(apath) == -1 && err == NULL)
987 err = got_error_from_errno("unlink");
988 if (afile != NULL && fclose(afile) == EOF && err == NULL)
989 err = got_error_from_errno("fclose");
990 free(apath);
992 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
993 err = got_error_from_errno("unlink");
994 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
995 err = got_error_from_errno("fclose");
996 free(mergepath);
998 free(newpath);
999 free(oldlabel);
1000 free(newlabel);
1001 free(anclabel);
1002 return err;
1005 const struct got_error *
1006 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1007 int nop, int strip, int reverse, struct got_object_id *commit_id,
1008 got_patch_progress_cb progress_cb, void *progress_arg,
1009 got_cancel_cb cancel_cb, void *cancel_arg)
1011 const struct got_error *err = NULL, *complete_err = NULL;
1012 struct got_fileindex *fileindex = NULL;
1013 struct got_commit_object *commit = NULL;
1014 struct got_tree_object *tree = NULL;
1015 char *fileindex_path = NULL;
1016 char *oldpath, *newpath;
1017 struct imsgbuf *ibuf;
1018 int imsg_fds[2] = {-1, -1};
1019 int overlapcnt, done = 0, failed = 0;
1020 pid_t pid;
1022 ibuf = calloc(1, sizeof(*ibuf));
1023 if (ibuf == NULL) {
1024 err = got_error_from_errno("calloc");
1025 goto done;
1028 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1029 err = got_error_from_errno("socketpair");
1030 goto done;
1033 pid = fork();
1034 if (pid == -1) {
1035 err = got_error_from_errno("fork");
1036 goto done;
1037 } else if (pid == 0) {
1038 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1039 NULL);
1040 /* not reached */
1043 if (close(imsg_fds[1]) == -1) {
1044 err = got_error_from_errno("close");
1045 goto done;
1047 imsg_fds[1] = -1;
1048 imsg_init(ibuf, imsg_fds[0]);
1050 err = send_patch(ibuf, fd);
1051 fd = -1;
1052 if (err)
1053 goto done;
1055 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1056 worktree);
1057 if (err)
1058 goto done;
1060 if (commit_id) {
1061 err = got_object_open_as_commit(&commit, repo, commit_id);
1062 if (err)
1063 goto done;
1065 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1066 if (err)
1067 goto done;
1070 while (!done && err == NULL) {
1071 struct got_patch p;
1072 struct patch_args pa;
1074 pa.progress_cb = progress_cb;
1075 pa.progress_arg = progress_arg;
1076 pa.head = &p.head;
1078 err = recv_patch(ibuf, &done, &p, strip);
1079 if (err || done)
1080 break;
1082 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1083 &newpath, worktree, repo, fileindex);
1084 if (err == NULL)
1085 err = apply_patch(&overlapcnt, worktree, repo,
1086 fileindex, oldpath, newpath, &p, nop, reverse,
1087 commit_id, tree, &pa, cancel_cb, cancel_arg);
1088 if (err != NULL) {
1089 failed = 1;
1090 /* recoverable errors */
1091 if (err->code == GOT_ERR_FILE_STATUS ||
1092 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1093 err = report_progress(&pa, p.old, p.new,
1094 GOT_STATUS_CANNOT_UPDATE, err);
1095 else if (err->code == GOT_ERR_HUNK_FAILED)
1096 err = report_progress(&pa, p.old, p.new,
1097 GOT_STATUS_CANNOT_UPDATE, NULL);
1099 if (overlapcnt != 0)
1100 failed = 1;
1102 free(oldpath);
1103 free(newpath);
1104 patch_free(&p);
1106 if (err)
1107 break;
1110 done:
1111 if (fileindex != NULL)
1112 complete_err = got_worktree_patch_complete(fileindex,
1113 fileindex_path);
1114 if (complete_err && err == NULL)
1115 err = complete_err;
1116 free(fileindex_path);
1117 if (tree)
1118 got_object_tree_close(tree);
1119 if (commit)
1120 got_object_commit_close(commit);
1121 if (fd != -1 && close(fd) == -1 && err == NULL)
1122 err = got_error_from_errno("close");
1123 if (ibuf != NULL)
1124 imsg_clear(ibuf);
1125 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1126 err = got_error_from_errno("close");
1127 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1128 err = got_error_from_errno("close");
1129 if (err == NULL && failed)
1130 err = got_error(GOT_ERR_PATCH_FAILED);
1131 return err;