Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 *
16 * Apply patches.
17 *
18 * Things that we may want to support:
19 * + support indented patches?
20 * + support other kinds of patches?
21 */
23 #include <sys/types.h>
24 #include <sys/queue.h>
25 #include <sys/socket.h>
26 #include <sys/stat.h>
27 #include <sys/uio.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_reference.h"
42 #include "got_cancel.h"
43 #include "got_worktree.h"
44 #include "got_repository.h"
45 #include "got_opentemp.h"
46 #include "got_patch.h"
47 #include "got_diff.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_diff.h"
51 #include "got_lib_object.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_sha1.h"
55 #define MIN(a, b) ((a) < (b) ? (a) : (b))
57 struct got_patch_hunk {
58 STAILQ_ENTRY(got_patch_hunk) entries;
59 const struct got_error *err;
60 int ws_mangled;
61 int offset;
62 int old_nonl;
63 int new_nonl;
64 int old_from;
65 int old_lines;
66 int new_from;
67 int new_lines;
68 size_t len;
69 size_t cap;
70 char **lines;
71 };
73 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
74 struct got_patch {
75 int xbit;
76 char *old;
77 char *new;
78 char cid[41];
79 char blob[41];
80 struct got_patch_hunk_head head;
81 };
83 struct patch_args {
84 got_patch_progress_cb progress_cb;
85 void *progress_arg;
86 struct got_patch_hunk_head *head;
87 };
89 static mode_t
90 apply_umask(mode_t mode)
91 {
92 mode_t um;
94 um = umask(000);
95 umask(um);
96 return mode & ~um;
97 }
99 static const struct got_error *
100 send_patch(struct imsgbuf *ibuf, int fd)
102 const struct got_error *err = NULL;
104 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
105 NULL, 0) == -1) {
106 err = got_error_from_errno(
107 "imsg_compose GOT_IMSG_PATCH_FILE");
108 close(fd);
109 return err;
112 return got_privsep_flush_imsg(ibuf);
115 static void
116 patch_free(struct got_patch *p)
118 struct got_patch_hunk *h;
119 size_t i;
121 while (!STAILQ_EMPTY(&p->head)) {
122 h = STAILQ_FIRST(&p->head);
123 STAILQ_REMOVE_HEAD(&p->head, entries);
125 for (i = 0; i < h->len; ++i)
126 free(h->lines[i]);
127 free(h->lines);
128 free(h);
131 free(p->new);
132 free(p->old);
134 memset(p, 0, sizeof(*p));
135 STAILQ_INIT(&p->head);
138 static const struct got_error *
139 pushline(struct got_patch_hunk *h, const char *line)
141 void *t;
142 size_t newcap;
144 if (h->len == h->cap) {
145 if ((newcap = h->cap * 1.5) == 0)
146 newcap = 16;
147 t = recallocarray(h->lines, h->cap, newcap,
148 sizeof(h->lines[0]));
149 if (t == NULL)
150 return got_error_from_errno("recallocarray");
151 h->lines = t;
152 h->cap = newcap;
155 if ((t = strdup(line)) == NULL)
156 return got_error_from_errno("strdup");
158 h->lines[h->len++] = t;
159 return NULL;
162 static const struct got_error *
163 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
165 const struct got_error *err = NULL;
166 struct imsg imsg;
167 struct got_imsg_patch_hunk hdr;
168 struct got_imsg_patch patch;
169 struct got_patch_hunk *h = NULL;
170 size_t datalen;
171 int lastmode = -1;
173 memset(p, 0, sizeof(*p));
174 STAILQ_INIT(&p->head);
176 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
177 if (err)
178 return err;
179 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
180 *done = 1;
181 goto done;
183 if (imsg.hdr.type != GOT_IMSG_PATCH) {
184 err = got_error(GOT_ERR_PRIVSEP_MSG);
185 goto done;
187 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
188 if (datalen != sizeof(patch)) {
189 err = got_error(GOT_ERR_PRIVSEP_LEN);
190 goto done;
192 memcpy(&patch, imsg.data, sizeof(patch));
194 if (patch.old[sizeof(patch.old)-1] != '\0' ||
195 patch.new[sizeof(patch.new)-1] != '\0' ||
196 patch.cid[sizeof(patch.cid)-1] != '\0' ||
197 patch.blob[sizeof(patch.blob)-1] != '\0') {
198 err = got_error(GOT_ERR_PRIVSEP_LEN);
199 goto done;
202 if (*patch.cid != '\0')
203 strlcpy(p->cid, patch.cid, sizeof(p->cid));
205 if (*patch.blob != '\0')
206 strlcpy(p->blob, patch.blob, sizeof(p->blob));
208 p->xbit = patch.xbit;
210 /* automatically set strip=1 for git-style diffs */
211 if (strip == -1 && patch.git &&
212 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
213 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
214 strip = 1;
216 /* prefer the new name if not /dev/null for not git-style diffs */
217 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
218 err = got_path_strip(&p->old, patch.new, strip);
219 if (err)
220 goto done;
221 } else if (*patch.old != '\0') {
222 err = got_path_strip(&p->old, patch.old, strip);
223 if (err)
224 goto done;
227 if (*patch.new != '\0') {
228 err = got_path_strip(&p->new, patch.new, strip);
229 if (err)
230 goto done;
233 if (p->old == NULL && p->new == NULL) {
234 err = got_error(GOT_ERR_PATCH_MALFORMED);
235 goto done;
238 imsg_free(&imsg);
240 for (;;) {
241 char *t;
243 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
244 if (err) {
245 patch_free(p);
246 return err;
249 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
250 switch (imsg.hdr.type) {
251 case GOT_IMSG_PATCH_DONE:
252 if (h != NULL && h->len == 0)
253 err = got_error(GOT_ERR_PATCH_MALFORMED);
254 goto done;
255 case GOT_IMSG_PATCH_HUNK:
256 if (h != NULL &&
257 (h->len == 0 || h->old_nonl || h->new_nonl)) {
258 err = got_error(GOT_ERR_PATCH_MALFORMED);
259 goto done;
261 lastmode = -1;
262 if (datalen != sizeof(hdr)) {
263 err = got_error(GOT_ERR_PRIVSEP_LEN);
264 goto done;
266 memcpy(&hdr, imsg.data, sizeof(hdr));
267 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
268 err = got_error(GOT_ERR_PRIVSEP_LEN);
269 goto done;
271 if ((h = calloc(1, sizeof(*h))) == NULL) {
272 err = got_error_from_errno("calloc");
273 goto done;
275 h->old_from = hdr.oldfrom;
276 h->old_lines = hdr.oldlines;
277 h->new_from = hdr.newfrom;
278 h->new_lines = hdr.newlines;
279 STAILQ_INSERT_TAIL(&p->head, h, entries);
280 break;
281 case GOT_IMSG_PATCH_LINE:
282 if (h == NULL) {
283 err = got_error(GOT_ERR_PRIVSEP_MSG);
284 goto done;
286 t = imsg.data;
287 /* at least one char */
288 if (datalen < 2 || t[datalen-1] != '\0') {
289 err = got_error(GOT_ERR_PRIVSEP_MSG);
290 goto done;
292 if (*t != ' ' && *t != '-' && *t != '+' &&
293 *t != '\\') {
294 err = got_error(GOT_ERR_PRIVSEP_MSG);
295 goto done;
298 if (*t != '\\')
299 err = pushline(h, t);
300 else if (lastmode == '-')
301 h->old_nonl = 1;
302 else if (lastmode == '+')
303 h->new_nonl = 1;
304 else
305 err = got_error(GOT_ERR_PATCH_MALFORMED);
307 if (err)
308 goto done;
310 lastmode = *t;
311 break;
312 default:
313 err = got_error(GOT_ERR_PRIVSEP_MSG);
314 goto done;
317 imsg_free(&imsg);
320 done:
321 if (err)
322 patch_free(p);
324 imsg_free(&imsg);
325 return err;
328 static void
329 reverse_patch(struct got_patch *p)
331 struct got_patch_hunk *h;
332 size_t i;
333 int tmp;
335 STAILQ_FOREACH(h, &p->head, entries) {
336 tmp = h->old_from;
337 h->old_from = h->new_from;
338 h->new_from = tmp;
340 tmp = h->old_lines;
341 h->old_lines = h->new_lines;
342 h->new_lines = tmp;
344 tmp = h->old_nonl;
345 h->old_nonl = h->new_nonl;
346 h->new_nonl = tmp;
348 for (i = 0; i < h->len; ++i) {
349 if (*h->lines[i] == '+')
350 *h->lines[i] = '-';
351 else if (*h->lines[i] == '-')
352 *h->lines[i] = '+';
357 /*
358 * Copy data from orig starting at copypos until pos into tmp.
359 * If pos is -1, copy until EOF.
360 */
361 static const struct got_error *
362 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
364 char buf[BUFSIZ];
365 size_t len, r, w;
367 if (fseeko(orig, copypos, SEEK_SET) == -1)
368 return got_error_from_errno("fseeko");
370 while (pos == -1 || copypos < pos) {
371 len = sizeof(buf);
372 if (pos > 0)
373 len = MIN(len, (size_t)pos - copypos);
374 r = fread(buf, 1, len, orig);
375 if (r != len && ferror(orig))
376 return got_error_from_errno("fread");
377 w = fwrite(buf, 1, r, tmp);
378 if (w != r)
379 return got_error_from_errno("fwrite");
380 copypos += len;
381 if (r != len && feof(orig)) {
382 if (pos == -1)
383 return NULL;
384 return got_error(GOT_ERR_HUNK_FAILED);
387 return NULL;
390 static int linecmp(const char *, const char *, int *);
392 static const struct got_error *
393 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
395 const struct got_error *err = NULL;
396 char *line = NULL;
397 char mode = *h->lines[0];
398 size_t linesize = 0;
399 ssize_t linelen;
400 off_t match = -1;
401 int mangled = 0, match_lineno = -1;
403 for (;;) {
404 linelen = getline(&line, &linesize, orig);
405 if (linelen == -1) {
406 if (ferror(orig))
407 err = got_error_from_errno("getline");
408 else if (match == -1)
409 err = got_error(GOT_ERR_HUNK_FAILED);
410 break;
412 if (line[linelen - 1] == '\n')
413 line[linelen - 1] = '\0';
414 (*lineno)++;
416 if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
417 (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
418 (mode == '+' && *lineno == h->old_from)) {
419 match = ftello(orig);
420 if (match == -1) {
421 err = got_error_from_errno("ftello");
422 break;
424 match -= linelen;
425 match_lineno = (*lineno)-1;
428 if (*lineno >= h->old_from && match != -1) {
429 if (mangled)
430 h->ws_mangled = 1;
431 break;
435 if (err == NULL) {
436 *pos = match;
437 *lineno = match_lineno;
438 if (fseeko(orig, match, SEEK_SET) == -1)
439 err = got_error_from_errno("fseeko");
442 free(line);
443 return err;
446 static int
447 linecmp(const char *a, const char *b, int *mangled)
449 int c;
451 *mangled = 0;
452 c = strcmp(a, b);
453 if (c == 0)
454 return c;
456 *mangled = 1;
457 for (;;) {
458 while (*a == '\t' || *a == ' ' || *a == '\f')
459 a++;
460 while (*b == '\t' || *b == ' ' || *b == '\f')
461 b++;
462 if (*a == '\0' || *a != *b)
463 break;
464 a++, b++;
467 return *a - *b;
470 static const struct got_error *
471 test_hunk(FILE *orig, struct got_patch_hunk *h)
473 const struct got_error *err = NULL;
474 char *line = NULL;
475 size_t linesize = 0, i = 0;
476 ssize_t linelen;
477 int mangled;
479 for (i = 0; i < h->len; ++i) {
480 switch (*h->lines[i]) {
481 case '+':
482 continue;
483 case ' ':
484 case '-':
485 linelen = getline(&line, &linesize, orig);
486 if (linelen == -1) {
487 if (ferror(orig))
488 err = got_error_from_errno("getline");
489 else
490 err = got_error(
491 GOT_ERR_HUNK_FAILED);
492 goto done;
494 if (line[linelen - 1] == '\n')
495 line[linelen - 1] = '\0';
496 if (linecmp(h->lines[i] + 1, line, &mangled)) {
497 err = got_error(GOT_ERR_HUNK_FAILED);
498 goto done;
500 if (mangled)
501 h->ws_mangled = 1;
502 break;
506 done:
507 free(line);
508 return err;
511 static const struct got_error *
512 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
513 off_t from)
515 const struct got_error *err = NULL;
516 const char *t;
517 size_t linesize = 0, i, new = 0;
518 char *line = NULL;
519 char mode;
520 ssize_t linelen;
522 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
523 return got_error_from_errno("fseeko");
525 for (i = 0; i < h->len; ++i) {
526 switch (mode = *h->lines[i]) {
527 case '-':
528 case ' ':
529 (*lineno)++;
530 if (orig != NULL) {
531 linelen = getline(&line, &linesize, orig);
532 if (linelen == -1) {
533 err = got_error_from_errno("getline");
534 goto done;
536 if (line[linelen - 1] == '\n')
537 line[linelen - 1] = '\0';
538 t = line;
539 } else
540 t = h->lines[i] + 1;
541 if (mode == '-')
542 continue;
543 if (fprintf(tmp, "%s\n", t) < 0) {
544 err = got_error_from_errno("fprintf");
545 goto done;
547 break;
548 case '+':
549 new++;
550 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
551 err = got_error_from_errno("fprintf");
552 goto done;
554 if (new != h->new_lines || !h->new_nonl) {
555 if (fprintf(tmp, "\n") < 0) {
556 err = got_error_from_errno("fprintf");
557 goto done;
560 break;
564 done:
565 free(line);
566 return err;
569 static const struct got_error *
570 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
572 const struct got_error *err = NULL;
573 struct got_patch_hunk *h;
574 struct stat sb;
575 int lineno = 0;
576 off_t copypos, pos;
577 char *line = NULL;
578 size_t linesize = 0;
579 ssize_t linelen;
581 if (p->old == NULL) { /* create */
582 h = STAILQ_FIRST(&p->head);
583 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
584 return got_error(GOT_ERR_PATCH_MALFORMED);
585 return apply_hunk(orig, tmp, h, &lineno, 0);
588 /* When deleting binary files there are no hunks to apply. */
589 if (p->new == NULL && STAILQ_EMPTY(&p->head))
590 return NULL;
592 if (fstat(fileno(orig), &sb) == -1)
593 return got_error_from_errno("fstat");
595 copypos = 0;
596 STAILQ_FOREACH(h, &p->head, entries) {
597 tryagain:
598 err = locate_hunk(orig, h, &pos, &lineno);
599 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
600 h->err = err;
601 if (err != NULL)
602 return err;
603 err = copy(tmp, orig, copypos, pos);
604 if (err != NULL)
605 return err;
606 copypos = pos;
608 err = test_hunk(orig, h);
609 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
610 /*
611 * try to apply the hunk again starting the search
612 * after the previous partial match.
613 */
614 if (fseeko(orig, pos, SEEK_SET) == -1)
615 return got_error_from_errno("fseeko");
616 linelen = getline(&line, &linesize, orig);
617 if (linelen == -1)
618 return got_error_from_errno("getline");
619 lineno++;
620 goto tryagain;
622 if (err != NULL)
623 return err;
625 if (lineno + 1 != h->old_from)
626 h->offset = lineno + 1 - h->old_from;
628 err = apply_hunk(orig, tmp, h, &lineno, pos);
629 if (err != NULL)
630 return err;
632 copypos = ftello(orig);
633 if (copypos == -1)
634 return got_error_from_errno("ftello");
637 if (p->new == NULL && sb.st_size != copypos) {
638 h = STAILQ_FIRST(&p->head);
639 h->err = got_error(GOT_ERR_HUNK_FAILED);
640 err = h->err;
641 } else if (!feof(orig))
642 err = copy(tmp, orig, copypos, -1);
644 return err;
647 static const struct got_error *
648 report_progress(struct patch_args *pa, const char *old, const char *new,
649 unsigned char status, const struct got_error *orig_error)
651 const struct got_error *err;
652 struct got_patch_hunk *h;
654 err = pa->progress_cb(pa->progress_arg, old, new, status,
655 orig_error, 0, 0, 0, 0, 0, 0, NULL);
656 if (err)
657 return err;
659 STAILQ_FOREACH(h, pa->head, entries) {
660 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
661 continue;
663 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
664 h->old_from, h->old_lines, h->new_from, h->new_lines,
665 h->offset, h->ws_mangled, h->err);
666 if (err)
667 return err;
670 return NULL;
673 static const struct got_error *
674 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
675 const char *path)
677 return report_progress(arg, path, NULL, status, NULL);
680 static const struct got_error *
681 patch_add(void *arg, unsigned char status, const char *path)
683 return report_progress(arg, NULL, path, status, NULL);
686 static const struct got_error *
687 open_blob(char **path, FILE **fp, const char *blobid,
688 struct got_repository *repo)
690 const struct got_error *err = NULL;
691 struct got_blob_object *blob = NULL;
692 struct got_object_id id, *idptr, *matched_id = NULL;
693 int fd = -1;
695 *fp = NULL;
696 *path = NULL;
698 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
699 err = got_repo_match_object_id(&matched_id, NULL, blobid,
700 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
701 repo);
702 if (err)
703 return err;
704 idptr = matched_id;
705 } else {
706 if (!got_parse_sha1_digest(id.sha1, blobid))
707 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
708 idptr = &id;
711 fd = got_opentempfd();
712 if (fd == -1) {
713 err = got_error_from_errno("got_opentempfd");
714 goto done;
717 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
718 if (err)
719 goto done;
721 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
722 "");
723 if (err)
724 goto done;
726 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
727 if (err)
728 goto done;
730 done:
731 if (fd != -1 && close(fd) == -1 && err == NULL)
732 err = got_error_from_errno("close");
733 if (blob)
734 got_object_blob_close(blob);
735 if (matched_id != NULL)
736 free(matched_id);
737 if (err) {
738 if (*fp != NULL)
739 fclose(*fp);
740 if (*path != NULL)
741 unlink(*path);
742 free(*path);
743 *fp = NULL;
744 *path = NULL;
746 return err;
749 static const struct got_error *
750 prepare_merge(int *do_merge, char **apath, FILE **afile,
751 struct got_worktree *worktree, struct got_repository *repo,
752 struct got_patch *p, struct got_object_id *commit_id,
753 struct got_tree_object *tree, const char *path)
755 const struct got_error *err = NULL;
757 *do_merge = 0;
758 *apath = NULL;
759 *afile = NULL;
761 /* don't run the diff3 merge on creations/deletions */
762 if (p->old == NULL || p->new == NULL)
763 return NULL;
765 if (commit_id) {
766 struct got_object_id *id;
768 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
769 if (err)
770 return err;
771 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
772 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
773 free(id);
774 err = open_blob(apath, afile, p->blob, repo);
775 *do_merge = err == NULL;
776 } else if (*p->blob != '\0') {
777 err = open_blob(apath, afile, p->blob, repo);
778 /*
779 * ignore failures to open this blob, we might have
780 * parsed gibberish.
781 */
782 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
783 err->code != GOT_ERR_NO_OBJ)
784 return err;
785 *do_merge = err == NULL;
786 err = NULL;
789 return err;
792 static const struct got_error *
793 apply_patch(int *overlapcnt, struct got_worktree *worktree,
794 struct got_repository *repo, struct got_fileindex *fileindex,
795 const char *old, const char *new, struct got_patch *p, int nop,
796 int reverse, struct got_object_id *commit_id,
797 struct got_tree_object *tree, struct patch_args *pa,
798 got_cancel_cb cancel_cb, void *cancel_arg)
800 const struct got_error *err = NULL;
801 struct stat sb;
802 int do_merge = 0, file_renamed = 0;
803 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
804 char *oldpath = NULL, *newpath = NULL;
805 char *tmppath = NULL, *template = NULL;
806 char *apath = NULL, *mergepath = NULL;
807 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
808 int outfd;
809 mode_t mode = GOT_DEFAULT_FILE_MODE;
811 *overlapcnt = 0;
813 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
814 commit_id, tree, old);
815 if (err)
816 return err;
818 if (reverse && !do_merge)
819 reverse_patch(p);
821 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
822 old) == -1) {
823 err = got_error_from_errno("asprintf");
824 goto done;
827 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
828 new) == -1) {
829 err = got_error_from_errno("asprintf");
830 goto done;
833 file_renamed = strcmp(oldpath, newpath);
835 if (asprintf(&template, "%s/got-patch",
836 got_worktree_get_root_path(worktree)) == -1) {
837 err = got_error_from_errno(template);
838 goto done;
841 if (p->old != NULL) {
842 if ((oldfile = fopen(oldpath, "r")) == NULL) {
843 err = got_error_from_errno2("open", oldpath);
844 goto done;
846 if (fstat(fileno(oldfile), &sb) == -1) {
847 err = got_error_from_errno2("fstat", oldpath);
848 goto done;
850 mode = sb.st_mode;
851 } else if (p->xbit)
852 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
854 err = got_opentemp_named(&tmppath, &tmpfile, template, "");
855 if (err)
856 goto done;
857 outfd = fileno(tmpfile);
858 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
859 if (err)
860 goto done;
862 if (do_merge) {
863 const char *type, *id;
865 if (fseeko(afile, 0, SEEK_SET) == -1 ||
866 fseeko(oldfile, 0, SEEK_SET) == -1 ||
867 fseeko(tmpfile, 0, SEEK_SET) == -1) {
868 err = got_error_from_errno("fseeko");
869 goto done;
872 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
873 err = got_error_from_errno("asprintf");
874 oldlabel = NULL;
875 goto done;
878 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
879 err = got_error_from_errno("asprintf");
880 newlabel = NULL;
881 goto done;
884 if (*p->cid != '\0') {
885 type = "commit";
886 id = p->cid;
887 } else {
888 type = "blob";
889 id = p->blob;
892 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
893 err = got_error_from_errno("asprintf");
894 anclabel = NULL;
895 goto done;
898 if (reverse) {
899 char *s;
900 FILE *t;
902 s = anclabel;
903 anclabel = newlabel;
904 newlabel = s;
906 t = afile;
907 afile = tmpfile;
908 tmpfile = t;
911 err = got_opentemp_named(&mergepath, &mergefile, template, "");
912 if (err)
913 goto done;
914 outfd = fileno(mergefile);
916 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
917 oldfile, oldlabel, anclabel, newlabel,
918 GOT_DIFF_ALGORITHM_PATIENCE);
919 if (err)
920 goto done;
923 if (nop)
924 goto done;
926 if (p->old != NULL && p->new == NULL) {
927 err = got_worktree_patch_schedule_rm(old, repo, worktree,
928 fileindex, patch_delete, pa);
929 goto done;
932 if (fchmod(outfd, apply_umask(mode)) == -1) {
933 err = got_error_from_errno2("chmod", tmppath);
934 goto done;
937 if (mergepath) {
938 err = got_path_move_file(mergepath, newpath);
939 if (err)
940 goto done;
941 free(mergepath);
942 mergepath = NULL;
943 } else {
944 err = got_path_move_file(tmppath, newpath);
945 if (err)
946 goto done;
947 free(tmppath);
948 tmppath = NULL;
951 if (file_renamed) {
952 err = got_worktree_patch_schedule_rm(old, repo, worktree,
953 fileindex, patch_delete, pa);
954 if (err == NULL)
955 err = got_worktree_patch_schedule_add(new, repo,
956 worktree, fileindex, patch_add,
957 pa);
958 if (err)
959 unlink(newpath);
960 } else if (p->old == NULL) {
961 err = got_worktree_patch_schedule_add(new, repo, worktree,
962 fileindex, patch_add, pa);
963 if (err)
964 unlink(newpath);
965 } else if (*overlapcnt != 0)
966 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
967 else if (do_merge)
968 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
969 else
970 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
972 done:
973 free(template);
975 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
976 err = got_error_from_errno("unlink");
977 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
978 err = got_error_from_errno("fclose");
979 free(tmppath);
981 free(oldpath);
982 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
983 err = got_error_from_errno("fclose");
985 if (apath != NULL && unlink(apath) == -1 && err == NULL)
986 err = got_error_from_errno("unlink");
987 if (afile != NULL && fclose(afile) == EOF && err == NULL)
988 err = got_error_from_errno("fclose");
989 free(apath);
991 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
992 err = got_error_from_errno("unlink");
993 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
994 err = got_error_from_errno("fclose");
995 free(mergepath);
997 free(newpath);
998 free(oldlabel);
999 free(newlabel);
1000 free(anclabel);
1001 return err;
1004 const struct got_error *
1005 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1006 int nop, int strip, int reverse, struct got_object_id *commit_id,
1007 got_patch_progress_cb progress_cb, void *progress_arg,
1008 got_cancel_cb cancel_cb, void *cancel_arg)
1010 const struct got_error *err = NULL, *complete_err = NULL;
1011 struct got_fileindex *fileindex = NULL;
1012 struct got_commit_object *commit = NULL;
1013 struct got_tree_object *tree = NULL;
1014 char *fileindex_path = NULL;
1015 char *oldpath, *newpath;
1016 struct imsgbuf *ibuf;
1017 int imsg_fds[2] = {-1, -1};
1018 int overlapcnt, done = 0, failed = 0;
1019 pid_t pid;
1021 ibuf = calloc(1, sizeof(*ibuf));
1022 if (ibuf == NULL) {
1023 err = got_error_from_errno("calloc");
1024 goto done;
1027 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1028 err = got_error_from_errno("socketpair");
1029 goto done;
1032 pid = fork();
1033 if (pid == -1) {
1034 err = got_error_from_errno("fork");
1035 goto done;
1036 } else if (pid == 0) {
1037 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1038 NULL);
1039 /* not reached */
1042 if (close(imsg_fds[1]) == -1) {
1043 err = got_error_from_errno("close");
1044 goto done;
1046 imsg_fds[1] = -1;
1047 imsg_init(ibuf, imsg_fds[0]);
1049 err = send_patch(ibuf, fd);
1050 fd = -1;
1051 if (err)
1052 goto done;
1054 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1055 worktree);
1056 if (err)
1057 goto done;
1059 if (commit_id) {
1060 err = got_object_open_as_commit(&commit, repo, commit_id);
1061 if (err)
1062 goto done;
1064 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1065 if (err)
1066 goto done;
1069 while (!done && err == NULL) {
1070 struct got_patch p;
1071 struct patch_args pa;
1073 pa.progress_cb = progress_cb;
1074 pa.progress_arg = progress_arg;
1075 pa.head = &p.head;
1077 err = recv_patch(ibuf, &done, &p, strip);
1078 if (err || done)
1079 break;
1081 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1082 &newpath, worktree, repo, fileindex);
1083 if (err == NULL)
1084 err = apply_patch(&overlapcnt, worktree, repo,
1085 fileindex, oldpath, newpath, &p, nop, reverse,
1086 commit_id, tree, &pa, cancel_cb, cancel_arg);
1087 if (err != NULL) {
1088 failed = 1;
1089 /* recoverable errors */
1090 if (err->code == GOT_ERR_FILE_STATUS ||
1091 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1092 err = report_progress(&pa, p.old, p.new,
1093 GOT_STATUS_CANNOT_UPDATE, err);
1094 else if (err->code == GOT_ERR_HUNK_FAILED)
1095 err = report_progress(&pa, p.old, p.new,
1096 GOT_STATUS_CANNOT_UPDATE, NULL);
1098 if (overlapcnt != 0)
1099 failed = 1;
1101 free(oldpath);
1102 free(newpath);
1103 patch_free(&p);
1105 if (err)
1106 break;
1109 done:
1110 if (fileindex != NULL)
1111 complete_err = got_worktree_patch_complete(fileindex,
1112 fileindex_path);
1113 if (complete_err && err == NULL)
1114 err = complete_err;
1115 free(fileindex_path);
1116 if (tree)
1117 got_object_tree_close(tree);
1118 if (commit)
1119 got_object_commit_close(commit);
1120 if (fd != -1 && close(fd) == -1 && err == NULL)
1121 err = got_error_from_errno("close");
1122 if (ibuf != NULL)
1123 imsg_clear(ibuf);
1124 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1125 err = got_error_from_errno("close");
1126 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1127 err = got_error_from_errno("close");
1128 if (err == NULL && failed)
1129 err = got_error(GOT_ERR_PATCH_FAILED);
1130 return err;