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 <sha2.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <imsg.h>
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_path.h"
44 #include "got_reference.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
47 #include "got_repository.h"
48 #include "got_opentemp.h"
49 #include "got_patch.h"
50 #include "got_diff.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_diff.h"
54 #include "got_lib_object.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_hash.h"
58 #ifndef MIN
59 #define MIN(a, b) ((a) < (b) ? (a) : (b))
60 #endif
62 struct got_patch_line {
63 char mode;
64 char *line;
65 size_t len;
66 };
68 struct got_patch_hunk {
69 STAILQ_ENTRY(got_patch_hunk) entries;
70 const struct got_error *err;
71 int ws_mangled;
72 int offset;
73 int old_nonl;
74 int new_nonl;
75 int old_from;
76 int old_lines;
77 int new_from;
78 int new_lines;
79 size_t len;
80 size_t cap;
81 struct got_patch_line *lines;
82 };
84 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
85 struct got_patch {
86 int xbit;
87 char *old;
88 char *new;
89 char cid[41];
90 char blob[41];
91 struct got_patch_hunk_head head;
92 };
94 struct patch_args {
95 got_patch_progress_cb progress_cb;
96 void *progress_arg;
97 struct got_patch_hunk_head *head;
98 };
100 static mode_t
101 apply_umask(mode_t mode)
103 mode_t um;
105 um = umask(000);
106 umask(um);
107 return mode & ~um;
110 static const struct got_error *
111 send_patch(struct imsgbuf *ibuf, int fd)
113 const struct got_error *err = NULL;
115 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
116 NULL, 0) == -1) {
117 err = got_error_from_errno(
118 "imsg_compose GOT_IMSG_PATCH_FILE");
119 close(fd);
120 return err;
123 return got_privsep_flush_imsg(ibuf);
126 static void
127 patch_free(struct got_patch *p)
129 struct got_patch_hunk *h;
130 size_t i;
132 while (!STAILQ_EMPTY(&p->head)) {
133 h = STAILQ_FIRST(&p->head);
134 STAILQ_REMOVE_HEAD(&p->head, entries);
136 for (i = 0; i < h->len; ++i)
137 free(h->lines[i].line);
138 free(h->lines);
139 free(h);
142 free(p->new);
143 free(p->old);
145 memset(p, 0, sizeof(*p));
146 STAILQ_INIT(&p->head);
149 static const struct got_error *
150 pushline(struct got_patch_hunk *h, const char *line, size_t len)
152 void *t;
153 size_t newcap;
155 if (h->len == h->cap) {
156 if ((newcap = h->cap * 1.5) == 0)
157 newcap = 16;
158 t = recallocarray(h->lines, h->cap, newcap,
159 sizeof(h->lines[0]));
160 if (t == NULL)
161 return got_error_from_errno("recallocarray");
162 h->lines = t;
163 h->cap = newcap;
166 if ((t = malloc(len - 1)) == NULL)
167 return got_error_from_errno("malloc");
168 memcpy(t, line + 1, len - 1); /* skip the line type */
170 h->lines[h->len].mode = *line;
171 h->lines[h->len].line = t;
172 h->lines[h->len].len = len - 2; /* line type and trailing NUL */
173 h->len++;
174 return NULL;
177 static const struct got_error *
178 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
180 const struct got_error *err = NULL;
181 struct imsg imsg;
182 struct got_imsg_patch_hunk hdr;
183 struct got_imsg_patch patch;
184 struct got_patch_hunk *h = NULL;
185 size_t datalen;
186 int lastmode = -1;
188 memset(p, 0, sizeof(*p));
189 STAILQ_INIT(&p->head);
191 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
192 if (err)
193 return err;
194 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
195 *done = 1;
196 goto done;
198 if (imsg.hdr.type != GOT_IMSG_PATCH) {
199 err = got_error(GOT_ERR_PRIVSEP_MSG);
200 goto done;
202 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
203 if (datalen != sizeof(patch)) {
204 err = got_error(GOT_ERR_PRIVSEP_LEN);
205 goto done;
207 memcpy(&patch, imsg.data, sizeof(patch));
209 if (patch.old[sizeof(patch.old)-1] != '\0' ||
210 patch.new[sizeof(patch.new)-1] != '\0' ||
211 patch.cid[sizeof(patch.cid)-1] != '\0' ||
212 patch.blob[sizeof(patch.blob)-1] != '\0') {
213 err = got_error(GOT_ERR_PRIVSEP_LEN);
214 goto done;
217 if (*patch.cid != '\0')
218 strlcpy(p->cid, patch.cid, sizeof(p->cid));
220 if (*patch.blob != '\0')
221 strlcpy(p->blob, patch.blob, sizeof(p->blob));
223 p->xbit = patch.xbit;
225 /* automatically set strip=1 for git-style diffs */
226 if (strip == -1 && patch.git &&
227 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
228 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
229 strip = 1;
231 /* prefer the new name if not /dev/null for not git-style diffs */
232 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
233 err = got_path_strip(&p->old, patch.new, strip);
234 if (err)
235 goto done;
236 } else if (*patch.old != '\0') {
237 err = got_path_strip(&p->old, patch.old, strip);
238 if (err)
239 goto done;
242 if (*patch.new != '\0') {
243 err = got_path_strip(&p->new, patch.new, strip);
244 if (err)
245 goto done;
248 if (p->old == NULL && p->new == NULL) {
249 err = got_error(GOT_ERR_PATCH_MALFORMED);
250 goto done;
253 imsg_free(&imsg);
255 for (;;) {
256 char *t;
258 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
259 if (err) {
260 patch_free(p);
261 return err;
264 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
265 switch (imsg.hdr.type) {
266 case GOT_IMSG_PATCH_DONE:
267 if (h != NULL && h->len == 0)
268 err = got_error(GOT_ERR_PATCH_MALFORMED);
269 goto done;
270 case GOT_IMSG_PATCH_HUNK:
271 if (h != NULL &&
272 (h->len == 0 || h->old_nonl || h->new_nonl)) {
273 err = got_error(GOT_ERR_PATCH_MALFORMED);
274 goto done;
276 lastmode = -1;
277 if (datalen != sizeof(hdr)) {
278 err = got_error(GOT_ERR_PRIVSEP_LEN);
279 goto done;
281 memcpy(&hdr, imsg.data, sizeof(hdr));
282 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
283 err = got_error(GOT_ERR_PRIVSEP_LEN);
284 goto done;
286 if ((h = calloc(1, sizeof(*h))) == NULL) {
287 err = got_error_from_errno("calloc");
288 goto done;
290 h->old_from = hdr.oldfrom;
291 h->old_lines = hdr.oldlines;
292 h->new_from = hdr.newfrom;
293 h->new_lines = hdr.newlines;
294 STAILQ_INSERT_TAIL(&p->head, h, entries);
295 break;
296 case GOT_IMSG_PATCH_LINE:
297 if (h == NULL) {
298 err = got_error(GOT_ERR_PRIVSEP_MSG);
299 goto done;
301 t = imsg.data;
302 /* at least one char */
303 if (datalen < 2 || t[datalen-1] != '\0') {
304 err = got_error(GOT_ERR_PRIVSEP_MSG);
305 goto done;
307 if (*t != ' ' && *t != '-' && *t != '+' &&
308 *t != '\\') {
309 err = got_error(GOT_ERR_PRIVSEP_MSG);
310 goto done;
313 if (*t != '\\')
314 err = pushline(h, t, datalen);
315 else if (lastmode == '-')
316 h->old_nonl = 1;
317 else if (lastmode == '+')
318 h->new_nonl = 1;
319 else
320 err = got_error(GOT_ERR_PATCH_MALFORMED);
322 if (err)
323 goto done;
325 lastmode = *t;
326 break;
327 default:
328 err = got_error(GOT_ERR_PRIVSEP_MSG);
329 goto done;
332 imsg_free(&imsg);
335 done:
336 if (err)
337 patch_free(p);
339 imsg_free(&imsg);
340 return err;
343 static void
344 reverse_patch(struct got_patch *p)
346 struct got_patch_hunk *h;
347 size_t i;
348 int tmp;
350 STAILQ_FOREACH(h, &p->head, entries) {
351 tmp = h->old_from;
352 h->old_from = h->new_from;
353 h->new_from = tmp;
355 tmp = h->old_lines;
356 h->old_lines = h->new_lines;
357 h->new_lines = tmp;
359 tmp = h->old_nonl;
360 h->old_nonl = h->new_nonl;
361 h->new_nonl = tmp;
363 for (i = 0; i < h->len; ++i) {
364 if (h->lines[i].mode == '+')
365 h->lines[i].mode = '-';
366 else if (h->lines[i].mode == '-')
367 h->lines[i].mode = '+';
372 /*
373 * Copy data from orig starting at copypos until pos into tmp.
374 * If pos is -1, copy until EOF.
375 */
376 static const struct got_error *
377 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
379 char buf[BUFSIZ];
380 size_t len, r, w;
382 if (fseeko(orig, copypos, SEEK_SET) == -1)
383 return got_error_from_errno("fseeko");
385 while (pos == -1 || copypos < pos) {
386 len = sizeof(buf);
387 if (pos > 0)
388 len = MIN(len, (size_t)pos - copypos);
389 r = fread(buf, 1, len, orig);
390 if (r != len && ferror(orig))
391 return got_error_from_errno("fread");
392 w = fwrite(buf, 1, r, tmp);
393 if (w != r)
394 return got_error_from_errno("fwrite");
395 copypos += len;
396 if (r != len && feof(orig)) {
397 if (pos == -1)
398 return NULL;
399 return got_error(GOT_ERR_HUNK_FAILED);
402 return NULL;
405 static int lines_eq(struct got_patch_line *, const char *, size_t, int *);
407 static const struct got_error *
408 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
410 const struct got_error *err = NULL;
411 struct got_patch_line *l = &h->lines[0];
412 char *line = NULL;
413 char mode = l->mode;
414 size_t linesize = 0;
415 ssize_t linelen;
416 off_t match = -1;
417 int mangled = 0, match_lineno = -1;
419 for (;;) {
420 linelen = getline(&line, &linesize, orig);
421 if (linelen == -1) {
422 if (ferror(orig))
423 err = got_error_from_errno("getline");
424 else if (match == -1)
425 err = got_error(GOT_ERR_HUNK_FAILED);
426 break;
428 (*lineno)++;
430 if ((mode == ' ' && lines_eq(l, line, linelen, &mangled)) ||
431 (mode == '-' && lines_eq(l, line, linelen, &mangled)) ||
432 (mode == '+' && *lineno == h->old_from)) {
433 match = ftello(orig);
434 if (match == -1) {
435 err = got_error_from_errno("ftello");
436 break;
438 match -= linelen;
439 match_lineno = (*lineno)-1;
442 if (*lineno >= h->old_from && match != -1) {
443 if (mangled)
444 h->ws_mangled = 1;
445 break;
449 if (err == NULL) {
450 *pos = match;
451 *lineno = match_lineno;
452 if (fseeko(orig, match, SEEK_SET) == -1)
453 err = got_error_from_errno("fseeko");
456 free(line);
457 return err;
460 static int
461 lines_eq(struct got_patch_line *l, const char *b, size_t len, int *mangled)
463 char *a = l->line;
464 size_t i, j;
466 if (len > 00 && b[len - 1] == '\n')
467 len--;
469 *mangled = 0;
470 if (l->len == len && !memcmp(a, b, len))
471 return 1;
473 *mangled = 1;
475 i = j = 0;
476 for (;;) {
477 while (i < l->len &&
478 (a[i] == '\t' || a[i] == ' ' || a[i] == '\f'))
479 i++;
480 while (j < len &&
481 (b[j] == '\t' || b[j] == ' ' || b[j] == '\f'))
482 j++;
483 if (i == l->len || j == len || a[i] != b[j])
484 break;
485 i++, j++;
488 return (i == l->len && j == len);
491 static const struct got_error *
492 test_hunk(FILE *orig, struct got_patch_hunk *h)
494 const struct got_error *err = NULL;
495 char *line = NULL;
496 size_t linesize = 0, i = 0;
497 ssize_t linelen;
498 int mangled;
500 for (i = 0; i < h->len; ++i) {
501 switch (h->lines[i].mode) {
502 case '+':
503 continue;
504 case ' ':
505 case '-':
506 linelen = getline(&line, &linesize, orig);
507 if (linelen == -1) {
508 if (ferror(orig))
509 err = got_error_from_errno("getline");
510 else
511 err = got_error(
512 GOT_ERR_HUNK_FAILED);
513 goto done;
515 if (!lines_eq(&h->lines[i], line, linelen, &mangled)) {
516 err = got_error(GOT_ERR_HUNK_FAILED);
517 goto done;
519 if (mangled)
520 h->ws_mangled = 1;
521 break;
525 done:
526 free(line);
527 return err;
530 static const struct got_error *
531 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
532 off_t from)
534 const struct got_error *err = NULL;
535 const char *t;
536 size_t linesize = 0, i, new = 0;
537 char *line = NULL;
538 char mode;
539 size_t l;
540 ssize_t linelen;
542 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
543 return got_error_from_errno("fseeko");
545 for (i = 0; i < h->len; ++i) {
546 switch (mode = h->lines[i].mode) {
547 case '-':
548 case ' ':
549 (*lineno)++;
550 if (orig != NULL) {
551 linelen = getline(&line, &linesize, orig);
552 if (linelen == -1) {
553 err = got_error_from_errno("getline");
554 goto done;
556 if (line[linelen - 1] == '\n')
557 line[linelen - 1] = '\0';
558 t = line;
559 l = linelen - 1;
560 } else {
561 t = h->lines[i].line;
562 l = h->lines[i].len;
564 if (mode == '-')
565 continue;
566 if (fwrite(t, 1, l, tmp) != l ||
567 fputc('\n', tmp) == EOF) {
568 err = got_error_from_errno("fprintf");
569 goto done;
571 break;
572 case '+':
573 new++;
574 t = h->lines[i].line;
575 l = h->lines[i].len;
576 if (fwrite(t, 1, l, tmp) != l) {
577 err = got_error_from_errno("fprintf");
578 goto done;
580 if (new != h->new_lines || !h->new_nonl) {
581 if (fprintf(tmp, "\n") < 0) {
582 err = got_error_from_errno("fprintf");
583 goto done;
586 break;
590 done:
591 free(line);
592 return err;
595 static const struct got_error *
596 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
598 const struct got_error *err = NULL;
599 struct got_patch_hunk *h;
600 struct stat sb;
601 int lineno = 0;
602 off_t copypos, pos;
603 char *line = NULL;
604 size_t linesize = 0;
605 ssize_t linelen;
607 if (p->old == NULL) { /* create */
608 h = STAILQ_FIRST(&p->head);
609 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
610 return got_error(GOT_ERR_PATCH_MALFORMED);
611 return apply_hunk(orig, tmp, h, &lineno, 0);
614 /* When deleting binary files there are no hunks to apply. */
615 if (p->new == NULL && STAILQ_EMPTY(&p->head))
616 return NULL;
618 if (fstat(fileno(orig), &sb) == -1)
619 return got_error_from_errno("fstat");
621 copypos = 0;
622 STAILQ_FOREACH(h, &p->head, entries) {
623 tryagain:
624 err = locate_hunk(orig, h, &pos, &lineno);
625 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
626 h->err = err;
627 if (err != NULL)
628 return err;
629 err = copy(tmp, orig, copypos, pos);
630 if (err != NULL)
631 return err;
632 copypos = pos;
634 err = test_hunk(orig, h);
635 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
636 /*
637 * try to apply the hunk again starting the search
638 * after the previous partial match.
639 */
640 if (fseeko(orig, pos, SEEK_SET) == -1)
641 return got_error_from_errno("fseeko");
642 linelen = getline(&line, &linesize, orig);
643 if (linelen == -1)
644 return got_error_from_errno("getline");
645 lineno++;
646 goto tryagain;
648 if (err != NULL)
649 return err;
651 if (lineno + 1 != h->old_from)
652 h->offset = lineno + 1 - h->old_from;
654 err = apply_hunk(orig, tmp, h, &lineno, pos);
655 if (err != NULL)
656 return err;
658 copypos = ftello(orig);
659 if (copypos == -1)
660 return got_error_from_errno("ftello");
663 if (p->new == NULL && sb.st_size != copypos) {
664 h = STAILQ_FIRST(&p->head);
665 h->err = got_error(GOT_ERR_HUNK_FAILED);
666 err = h->err;
667 } else if (!feof(orig))
668 err = copy(tmp, orig, copypos, -1);
670 return err;
673 static const struct got_error *
674 report_progress(struct patch_args *pa, const char *old, const char *new,
675 unsigned char status, const struct got_error *orig_error)
677 const struct got_error *err;
678 struct got_patch_hunk *h;
680 err = pa->progress_cb(pa->progress_arg, old, new, status,
681 orig_error, 0, 0, 0, 0, 0, 0, NULL);
682 if (err)
683 return err;
685 STAILQ_FOREACH(h, pa->head, entries) {
686 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
687 continue;
689 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
690 h->old_from, h->old_lines, h->new_from, h->new_lines,
691 h->offset, h->ws_mangled, h->err);
692 if (err)
693 return err;
696 return NULL;
699 static const struct got_error *
700 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
701 const char *path)
703 return report_progress(arg, path, NULL, status, NULL);
706 static const struct got_error *
707 patch_add(void *arg, unsigned char status, const char *path)
709 return report_progress(arg, NULL, path, status, NULL);
712 static const struct got_error *
713 open_blob(char **path, FILE **fp, const char *blobid,
714 struct got_repository *repo)
716 const struct got_error *err = NULL;
717 struct got_blob_object *blob = NULL;
718 struct got_object_id id, *idptr, *matched_id = NULL;
719 int fd = -1;
721 *fp = NULL;
722 *path = NULL;
724 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
725 err = got_repo_match_object_id(&matched_id, NULL, blobid,
726 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
727 repo);
728 if (err)
729 return err;
730 idptr = matched_id;
731 } else {
732 if (!got_parse_object_id(&id, blobid, GOT_HASH_SHA1))
733 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
734 idptr = &id;
737 fd = got_opentempfd();
738 if (fd == -1) {
739 err = got_error_from_errno("got_opentempfd");
740 goto done;
743 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
744 if (err)
745 goto done;
747 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
748 "");
749 if (err)
750 goto done;
752 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
753 if (err)
754 goto done;
756 done:
757 if (fd != -1 && close(fd) == -1 && err == NULL)
758 err = got_error_from_errno("close");
759 if (blob)
760 got_object_blob_close(blob);
761 if (matched_id != NULL)
762 free(matched_id);
763 if (err) {
764 if (*fp != NULL)
765 fclose(*fp);
766 if (*path != NULL)
767 unlink(*path);
768 free(*path);
769 *fp = NULL;
770 *path = NULL;
772 return err;
775 static const struct got_error *
776 prepare_merge(int *do_merge, char **apath, FILE **afile,
777 struct got_worktree *worktree, struct got_repository *repo,
778 struct got_patch *p, struct got_object_id *commit_id,
779 struct got_tree_object *tree, const char *path)
781 const struct got_error *err = NULL;
783 *do_merge = 0;
784 *apath = NULL;
785 *afile = NULL;
787 /* don't run the diff3 merge on creations/deletions */
788 if (p->old == NULL || p->new == NULL)
789 return NULL;
791 if (commit_id) {
792 struct got_object_id *id;
794 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
795 if (err)
796 return err;
797 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
798 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
799 free(id);
800 err = open_blob(apath, afile, p->blob, repo);
801 *do_merge = err == NULL;
802 } else if (*p->blob != '\0') {
803 err = open_blob(apath, afile, p->blob, repo);
804 /*
805 * ignore failures to open this blob, we might have
806 * parsed gibberish.
807 */
808 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
809 err->code != GOT_ERR_NO_OBJ)
810 return err;
811 *do_merge = err == NULL;
812 err = NULL;
815 return err;
818 static const struct got_error *
819 apply_patch(int *overlapcnt, struct got_worktree *worktree,
820 struct got_repository *repo, struct got_fileindex *fileindex,
821 const char *old, const char *new, struct got_patch *p, int nop,
822 int reverse, struct got_object_id *commit_id,
823 struct got_tree_object *tree, struct patch_args *pa,
824 got_cancel_cb cancel_cb, void *cancel_arg)
826 const struct got_error *err = NULL;
827 struct stat sb;
828 int do_merge = 0, file_renamed = 0;
829 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
830 char *oldpath = NULL, *newpath = NULL;
831 char *tmppath = NULL, *template = NULL;
832 char *apath = NULL, *mergepath = NULL;
833 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
834 int outfd;
835 mode_t mode = GOT_DEFAULT_FILE_MODE;
837 *overlapcnt = 0;
839 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
840 commit_id, tree, old);
841 if (err)
842 return err;
844 if (reverse && !do_merge)
845 reverse_patch(p);
847 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
848 old) == -1) {
849 err = got_error_from_errno("asprintf");
850 goto done;
853 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
854 new) == -1) {
855 err = got_error_from_errno("asprintf");
856 goto done;
859 file_renamed = strcmp(oldpath, newpath);
861 if (asprintf(&template, "%s/got-patch",
862 got_worktree_get_root_path(worktree)) == -1) {
863 err = got_error_from_errno(template);
864 goto done;
867 if (p->old != NULL) {
868 if ((oldfile = fopen(oldpath, "r")) == NULL) {
869 err = got_error_from_errno2("open", oldpath);
870 goto done;
872 if (fstat(fileno(oldfile), &sb) == -1) {
873 err = got_error_from_errno2("fstat", oldpath);
874 goto done;
876 mode = sb.st_mode;
877 } else if (p->xbit)
878 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
880 err = got_opentemp_named(&tmppath, &tmpfile, template, "");
881 if (err)
882 goto done;
883 outfd = fileno(tmpfile);
884 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
885 if (err)
886 goto done;
888 if (do_merge) {
889 const char *type, *id;
891 if (fseeko(afile, 0, SEEK_SET) == -1 ||
892 fseeko(oldfile, 0, SEEK_SET) == -1 ||
893 fseeko(tmpfile, 0, SEEK_SET) == -1) {
894 err = got_error_from_errno("fseeko");
895 goto done;
898 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
899 err = got_error_from_errno("asprintf");
900 oldlabel = NULL;
901 goto done;
904 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
905 err = got_error_from_errno("asprintf");
906 newlabel = NULL;
907 goto done;
910 if (*p->cid != '\0') {
911 type = "commit";
912 id = p->cid;
913 } else {
914 type = "blob";
915 id = p->blob;
918 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
919 err = got_error_from_errno("asprintf");
920 anclabel = NULL;
921 goto done;
924 if (reverse) {
925 char *s;
926 FILE *t;
928 s = anclabel;
929 anclabel = newlabel;
930 newlabel = s;
932 t = afile;
933 afile = tmpfile;
934 tmpfile = t;
937 err = got_opentemp_named(&mergepath, &mergefile, template, "");
938 if (err)
939 goto done;
940 outfd = fileno(mergefile);
942 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
943 oldfile, oldlabel, anclabel, newlabel,
944 GOT_DIFF_ALGORITHM_PATIENCE);
945 if (err)
946 goto done;
949 if (nop)
950 goto done;
952 if (p->old != NULL && p->new == NULL) {
953 err = got_worktree_patch_schedule_rm(old, repo, worktree,
954 fileindex, patch_delete, pa);
955 goto done;
958 if (fchmod(outfd, apply_umask(mode)) == -1) {
959 err = got_error_from_errno2("chmod", tmppath);
960 goto done;
963 if (mergepath) {
964 err = got_path_move_file(mergepath, newpath);
965 if (err)
966 goto done;
967 free(mergepath);
968 mergepath = NULL;
969 } else {
970 err = got_path_move_file(tmppath, newpath);
971 if (err)
972 goto done;
973 free(tmppath);
974 tmppath = NULL;
977 if (file_renamed) {
978 err = got_worktree_patch_schedule_rm(old, repo, worktree,
979 fileindex, patch_delete, pa);
980 if (err == NULL)
981 err = got_worktree_patch_schedule_add(new, repo,
982 worktree, fileindex, patch_add,
983 pa);
984 if (err)
985 unlink(newpath);
986 } else if (p->old == NULL) {
987 err = got_worktree_patch_schedule_add(new, repo, worktree,
988 fileindex, patch_add, pa);
989 if (err)
990 unlink(newpath);
991 } else if (*overlapcnt != 0)
992 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
993 else if (do_merge)
994 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
995 else
996 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
998 done:
999 free(template);
1001 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1002 err = got_error_from_errno("unlink");
1003 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
1004 err = got_error_from_errno("fclose");
1005 free(tmppath);
1007 free(oldpath);
1008 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
1009 err = got_error_from_errno("fclose");
1011 if (apath != NULL && unlink(apath) == -1 && err == NULL)
1012 err = got_error_from_errno("unlink");
1013 if (afile != NULL && fclose(afile) == EOF && err == NULL)
1014 err = got_error_from_errno("fclose");
1015 free(apath);
1017 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
1018 err = got_error_from_errno("unlink");
1019 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
1020 err = got_error_from_errno("fclose");
1021 free(mergepath);
1023 free(newpath);
1024 free(oldlabel);
1025 free(newlabel);
1026 free(anclabel);
1027 return err;
1030 const struct got_error *
1031 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1032 int nop, int strip, int reverse, struct got_object_id *commit_id,
1033 got_patch_progress_cb progress_cb, void *progress_arg,
1034 got_cancel_cb cancel_cb, void *cancel_arg)
1036 const struct got_error *err = NULL, *complete_err = NULL;
1037 struct got_fileindex *fileindex = NULL;
1038 struct got_commit_object *commit = NULL;
1039 struct got_tree_object *tree = NULL;
1040 char *fileindex_path = NULL;
1041 char *oldpath, *newpath;
1042 struct imsgbuf *ibuf;
1043 int imsg_fds[2] = {-1, -1};
1044 int overlapcnt, done = 0, failed = 0;
1045 pid_t pid;
1047 ibuf = calloc(1, sizeof(*ibuf));
1048 if (ibuf == NULL) {
1049 err = got_error_from_errno("calloc");
1050 goto done;
1053 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1054 err = got_error_from_errno("socketpair");
1055 goto done;
1058 pid = fork();
1059 if (pid == -1) {
1060 err = got_error_from_errno("fork");
1061 goto done;
1062 } else if (pid == 0) {
1063 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1064 NULL);
1065 /* not reached */
1068 if (close(imsg_fds[1]) == -1) {
1069 err = got_error_from_errno("close");
1070 goto done;
1072 imsg_fds[1] = -1;
1073 imsg_init(ibuf, imsg_fds[0]);
1075 err = send_patch(ibuf, fd);
1076 fd = -1;
1077 if (err)
1078 goto done;
1080 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1081 worktree);
1082 if (err)
1083 goto done;
1085 if (commit_id) {
1086 err = got_object_open_as_commit(&commit, repo, commit_id);
1087 if (err)
1088 goto done;
1090 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1091 if (err)
1092 goto done;
1095 while (!done && err == NULL) {
1096 struct got_patch p;
1097 struct patch_args pa;
1099 pa.progress_cb = progress_cb;
1100 pa.progress_arg = progress_arg;
1101 pa.head = &p.head;
1103 err = recv_patch(ibuf, &done, &p, strip);
1104 if (err || done)
1105 break;
1107 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1108 &newpath, worktree, repo, fileindex);
1109 if (err == NULL)
1110 err = apply_patch(&overlapcnt, worktree, repo,
1111 fileindex, oldpath, newpath, &p, nop, reverse,
1112 commit_id, tree, &pa, cancel_cb, cancel_arg);
1113 if (err != NULL) {
1114 failed = 1;
1115 /* recoverable errors */
1116 if (err->code == GOT_ERR_FILE_STATUS ||
1117 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1118 err = report_progress(&pa, p.old, p.new,
1119 GOT_STATUS_CANNOT_UPDATE, err);
1120 else if (err->code == GOT_ERR_HUNK_FAILED)
1121 err = report_progress(&pa, p.old, p.new,
1122 GOT_STATUS_CANNOT_UPDATE, NULL);
1124 if (overlapcnt != 0)
1125 failed = 1;
1127 free(oldpath);
1128 free(newpath);
1129 patch_free(&p);
1131 if (err)
1132 break;
1135 done:
1136 if (fileindex != NULL)
1137 complete_err = got_worktree_patch_complete(fileindex,
1138 fileindex_path);
1139 if (complete_err && err == NULL)
1140 err = complete_err;
1141 free(fileindex_path);
1142 if (tree)
1143 got_object_tree_close(tree);
1144 if (commit)
1145 got_object_commit_close(commit);
1146 if (fd != -1 && close(fd) == -1 && err == NULL)
1147 err = got_error_from_errno("close");
1148 if (ibuf != NULL)
1149 imsg_clear(ibuf);
1150 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1151 err = got_error_from_errno("close");
1152 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1153 err = got_error_from_errno("close");
1154 if (err == NULL && failed)
1155 err = got_error(GOT_ERR_PATCH_FAILED);
1156 return err;