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 const struct got_error *
92 send_patch(struct imsgbuf *ibuf, int fd)
93 {
94 const struct got_error *err = NULL;
96 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
97 NULL, 0) == -1) {
98 err = got_error_from_errno(
99 "imsg_compose GOT_IMSG_PATCH_FILE");
100 close(fd);
101 return err;
104 return got_privsep_flush_imsg(ibuf);
107 static void
108 patch_free(struct got_patch *p)
110 struct got_patch_hunk *h;
111 size_t i;
113 while (!STAILQ_EMPTY(&p->head)) {
114 h = STAILQ_FIRST(&p->head);
115 STAILQ_REMOVE_HEAD(&p->head, entries);
117 for (i = 0; i < h->len; ++i)
118 free(h->lines[i]);
119 free(h->lines);
120 free(h);
123 free(p->new);
124 free(p->old);
126 memset(p, 0, sizeof(*p));
127 STAILQ_INIT(&p->head);
130 static const struct got_error *
131 pushline(struct got_patch_hunk *h, const char *line)
133 void *t;
134 size_t newcap;
136 if (h->len == h->cap) {
137 if ((newcap = h->cap * 1.5) == 0)
138 newcap = 16;
139 t = recallocarray(h->lines, h->cap, newcap,
140 sizeof(h->lines[0]));
141 if (t == NULL)
142 return got_error_from_errno("recallocarray");
143 h->lines = t;
144 h->cap = newcap;
147 if ((t = strdup(line)) == NULL)
148 return got_error_from_errno("strdup");
150 h->lines[h->len++] = t;
151 return NULL;
154 static const struct got_error *
155 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
157 const struct got_error *err = NULL;
158 struct imsg imsg;
159 struct got_imsg_patch_hunk hdr;
160 struct got_imsg_patch patch;
161 struct got_patch_hunk *h = NULL;
162 size_t datalen;
163 int lastmode = -1;
165 memset(p, 0, sizeof(*p));
166 STAILQ_INIT(&p->head);
168 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
169 if (err)
170 return err;
171 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
172 *done = 1;
173 goto done;
175 if (imsg.hdr.type != GOT_IMSG_PATCH) {
176 err = got_error(GOT_ERR_PRIVSEP_MSG);
177 goto done;
179 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
180 if (datalen != sizeof(patch)) {
181 err = got_error(GOT_ERR_PRIVSEP_LEN);
182 goto done;
184 memcpy(&patch, imsg.data, sizeof(patch));
186 if (patch.old[sizeof(patch.old)-1] != '\0' ||
187 patch.new[sizeof(patch.new)-1] != '\0' ||
188 patch.cid[sizeof(patch.cid)-1] != '\0' ||
189 patch.blob[sizeof(patch.blob)-1] != '\0') {
190 err = got_error(GOT_ERR_PRIVSEP_LEN);
191 goto done;
194 if (*patch.cid != '\0')
195 strlcpy(p->cid, patch.cid, sizeof(p->cid));
197 if (*patch.blob != '\0')
198 strlcpy(p->blob, patch.blob, sizeof(p->blob));
200 p->xbit = patch.xbit;
202 /* automatically set strip=1 for git-style diffs */
203 if (strip == -1 && patch.git &&
204 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
205 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
206 strip = 1;
208 /* prefer the new name if not /dev/null for not git-style diffs */
209 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
210 err = got_path_strip(&p->old, patch.new, strip);
211 if (err)
212 goto done;
213 } else if (*patch.old != '\0') {
214 err = got_path_strip(&p->old, patch.old, strip);
215 if (err)
216 goto done;
219 if (*patch.new != '\0') {
220 err = got_path_strip(&p->new, patch.new, strip);
221 if (err)
222 goto done;
225 if (p->old == NULL && p->new == NULL) {
226 err = got_error(GOT_ERR_PATCH_MALFORMED);
227 goto done;
230 imsg_free(&imsg);
232 for (;;) {
233 char *t;
235 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
236 if (err) {
237 patch_free(p);
238 return err;
241 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
242 switch (imsg.hdr.type) {
243 case GOT_IMSG_PATCH_DONE:
244 if (h != NULL && h->len == 0)
245 err = got_error(GOT_ERR_PATCH_MALFORMED);
246 goto done;
247 case GOT_IMSG_PATCH_HUNK:
248 if (h != NULL &&
249 (h->len == 0 || h->old_nonl || h->new_nonl)) {
250 err = got_error(GOT_ERR_PATCH_MALFORMED);
251 goto done;
253 lastmode = -1;
254 if (datalen != sizeof(hdr)) {
255 err = got_error(GOT_ERR_PRIVSEP_LEN);
256 goto done;
258 memcpy(&hdr, imsg.data, sizeof(hdr));
259 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
260 err = got_error(GOT_ERR_PRIVSEP_LEN);
261 goto done;
263 if ((h = calloc(1, sizeof(*h))) == NULL) {
264 err = got_error_from_errno("calloc");
265 goto done;
267 h->old_from = hdr.oldfrom;
268 h->old_lines = hdr.oldlines;
269 h->new_from = hdr.newfrom;
270 h->new_lines = hdr.newlines;
271 STAILQ_INSERT_TAIL(&p->head, h, entries);
272 break;
273 case GOT_IMSG_PATCH_LINE:
274 if (h == NULL) {
275 err = got_error(GOT_ERR_PRIVSEP_MSG);
276 goto done;
278 t = imsg.data;
279 /* at least one char */
280 if (datalen < 2 || t[datalen-1] != '\0') {
281 err = got_error(GOT_ERR_PRIVSEP_MSG);
282 goto done;
284 if (*t != ' ' && *t != '-' && *t != '+' &&
285 *t != '\\') {
286 err = got_error(GOT_ERR_PRIVSEP_MSG);
287 goto done;
290 if (*t != '\\')
291 err = pushline(h, t);
292 else if (lastmode == '-')
293 h->old_nonl = 1;
294 else if (lastmode == '+')
295 h->new_nonl = 1;
296 else
297 err = got_error(GOT_ERR_PATCH_MALFORMED);
299 if (err)
300 goto done;
302 lastmode = *t;
303 break;
304 default:
305 err = got_error(GOT_ERR_PRIVSEP_MSG);
306 goto done;
309 imsg_free(&imsg);
312 done:
313 if (err)
314 patch_free(p);
316 imsg_free(&imsg);
317 return err;
320 static void
321 reverse_patch(struct got_patch *p)
323 struct got_patch_hunk *h;
324 size_t i;
325 int tmp;
327 STAILQ_FOREACH(h, &p->head, entries) {
328 tmp = h->old_from;
329 h->old_from = h->new_from;
330 h->new_from = tmp;
332 tmp = h->old_lines;
333 h->old_lines = h->new_lines;
334 h->new_lines = tmp;
336 tmp = h->old_nonl;
337 h->old_nonl = h->new_nonl;
338 h->new_nonl = tmp;
340 for (i = 0; i < h->len; ++i) {
341 if (*h->lines[i] == '+')
342 *h->lines[i] = '-';
343 else if (*h->lines[i] == '-')
344 *h->lines[i] = '+';
349 /*
350 * Copy data from orig starting at copypos until pos into tmp.
351 * If pos is -1, copy until EOF.
352 */
353 static const struct got_error *
354 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
356 char buf[BUFSIZ];
357 size_t len, r, w;
359 if (fseeko(orig, copypos, SEEK_SET) == -1)
360 return got_error_from_errno("fseeko");
362 while (pos == -1 || copypos < pos) {
363 len = sizeof(buf);
364 if (pos > 0)
365 len = MIN(len, (size_t)pos - copypos);
366 r = fread(buf, 1, len, orig);
367 if (r != len && ferror(orig))
368 return got_error_from_errno("fread");
369 w = fwrite(buf, 1, r, tmp);
370 if (w != r)
371 return got_error_from_errno("fwrite");
372 copypos += len;
373 if (r != len && feof(orig)) {
374 if (pos == -1)
375 return NULL;
376 return got_error(GOT_ERR_HUNK_FAILED);
379 return NULL;
382 static int linecmp(const char *, const char *, int *);
384 static const struct got_error *
385 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
387 const struct got_error *err = NULL;
388 char *line = NULL;
389 char mode = *h->lines[0];
390 size_t linesize = 0;
391 ssize_t linelen;
392 off_t match = -1;
393 int mangled = 0, match_lineno = -1;
395 for (;;) {
396 linelen = getline(&line, &linesize, orig);
397 if (linelen == -1) {
398 if (ferror(orig))
399 err = got_error_from_errno("getline");
400 else if (match == -1)
401 err = got_error(GOT_ERR_HUNK_FAILED);
402 break;
404 if (line[linelen - 1] == '\n')
405 line[linelen - 1] = '\0';
406 (*lineno)++;
408 if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
409 (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
410 (mode == '+' && *lineno == h->old_from)) {
411 match = ftello(orig);
412 if (match == -1) {
413 err = got_error_from_errno("ftello");
414 break;
416 match -= linelen;
417 match_lineno = (*lineno)-1;
420 if (*lineno >= h->old_from && match != -1) {
421 if (mangled)
422 h->ws_mangled = 1;
423 break;
427 if (err == NULL) {
428 *pos = match;
429 *lineno = match_lineno;
430 if (fseeko(orig, match, SEEK_SET) == -1)
431 err = got_error_from_errno("fseeko");
434 free(line);
435 return err;
438 static int
439 linecmp(const char *a, const char *b, int *mangled)
441 int c;
443 *mangled = 0;
444 c = strcmp(a, b);
445 if (c == 0)
446 return c;
448 *mangled = 1;
449 for (;;) {
450 while (*a == '\t' || *a == ' ' || *a == '\f')
451 a++;
452 while (*b == '\t' || *b == ' ' || *b == '\f')
453 b++;
454 if (*a == '\0' || *a != *b)
455 break;
456 a++, b++;
459 return *a - *b;
462 static const struct got_error *
463 test_hunk(FILE *orig, struct got_patch_hunk *h)
465 const struct got_error *err = NULL;
466 char *line = NULL;
467 size_t linesize = 0, i = 0;
468 ssize_t linelen;
469 int mangled;
471 for (i = 0; i < h->len; ++i) {
472 switch (*h->lines[i]) {
473 case '+':
474 continue;
475 case ' ':
476 case '-':
477 linelen = getline(&line, &linesize, orig);
478 if (linelen == -1) {
479 if (ferror(orig))
480 err = got_error_from_errno("getline");
481 else
482 err = got_error(
483 GOT_ERR_HUNK_FAILED);
484 goto done;
486 if (line[linelen - 1] == '\n')
487 line[linelen - 1] = '\0';
488 if (linecmp(h->lines[i] + 1, line, &mangled)) {
489 err = got_error(GOT_ERR_HUNK_FAILED);
490 goto done;
492 if (mangled)
493 h->ws_mangled = 1;
494 break;
498 done:
499 free(line);
500 return err;
503 static const struct got_error *
504 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
505 off_t from)
507 const struct got_error *err = NULL;
508 const char *t;
509 size_t linesize = 0, i, new = 0;
510 char *line = NULL;
511 char mode;
512 ssize_t linelen;
514 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
515 return got_error_from_errno("fseeko");
517 for (i = 0; i < h->len; ++i) {
518 switch (mode = *h->lines[i]) {
519 case '-':
520 case ' ':
521 (*lineno)++;
522 if (orig != NULL) {
523 linelen = getline(&line, &linesize, orig);
524 if (linelen == -1) {
525 err = got_error_from_errno("getline");
526 goto done;
528 if (line[linelen - 1] == '\n')
529 line[linelen - 1] = '\0';
530 t = line;
531 } else
532 t = h->lines[i] + 1;
533 if (mode == '-')
534 continue;
535 if (fprintf(tmp, "%s\n", t) < 0) {
536 err = got_error_from_errno("fprintf");
537 goto done;
539 break;
540 case '+':
541 new++;
542 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
543 err = got_error_from_errno("fprintf");
544 goto done;
546 if (new != h->new_lines || !h->new_nonl) {
547 if (fprintf(tmp, "\n") < 0) {
548 err = got_error_from_errno("fprintf");
549 goto done;
552 break;
556 done:
557 free(line);
558 return err;
561 static const struct got_error *
562 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
564 const struct got_error *err = NULL;
565 struct got_patch_hunk *h;
566 struct stat sb;
567 int lineno = 0;
568 off_t copypos, pos;
569 char *line = NULL;
570 size_t linesize = 0;
571 ssize_t linelen;
573 if (p->old == NULL) { /* create */
574 h = STAILQ_FIRST(&p->head);
575 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
576 return got_error(GOT_ERR_PATCH_MALFORMED);
577 return apply_hunk(orig, tmp, h, &lineno, 0);
580 if (fstat(fileno(orig), &sb) == -1)
581 return got_error_from_errno("fstat");
583 copypos = 0;
584 STAILQ_FOREACH(h, &p->head, entries) {
585 tryagain:
586 err = locate_hunk(orig, h, &pos, &lineno);
587 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
588 h->err = err;
589 if (err != NULL)
590 return err;
591 err = copy(tmp, orig, copypos, pos);
592 if (err != NULL)
593 return err;
594 copypos = pos;
596 err = test_hunk(orig, h);
597 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
598 /*
599 * try to apply the hunk again starting the search
600 * after the previous partial match.
601 */
602 if (fseeko(orig, pos, SEEK_SET) == -1)
603 return got_error_from_errno("fseeko");
604 linelen = getline(&line, &linesize, orig);
605 if (linelen == -1)
606 return got_error_from_errno("getline");
607 lineno++;
608 goto tryagain;
610 if (err != NULL)
611 return err;
613 if (lineno + 1 != h->old_from)
614 h->offset = lineno + 1 - h->old_from;
616 err = apply_hunk(orig, tmp, h, &lineno, pos);
617 if (err != NULL)
618 return err;
620 copypos = ftello(orig);
621 if (copypos == -1)
622 return got_error_from_errno("ftello");
625 if (p->new == NULL && sb.st_size != copypos) {
626 h = STAILQ_FIRST(&p->head);
627 h->err = got_error(GOT_ERR_HUNK_FAILED);
628 err = h->err;
629 } else if (!feof(orig))
630 err = copy(tmp, orig, copypos, -1);
632 return err;
635 static const struct got_error *
636 report_progress(struct patch_args *pa, const char *old, const char *new,
637 unsigned char status, const struct got_error *orig_error)
639 const struct got_error *err;
640 struct got_patch_hunk *h;
642 err = pa->progress_cb(pa->progress_arg, old, new, status,
643 orig_error, 0, 0, 0, 0, 0, 0, NULL);
644 if (err)
645 return err;
647 STAILQ_FOREACH(h, pa->head, entries) {
648 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
649 continue;
651 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
652 h->old_from, h->old_lines, h->new_from, h->new_lines,
653 h->offset, h->ws_mangled, h->err);
654 if (err)
655 return err;
658 return NULL;
661 static const struct got_error *
662 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
663 const char *path)
665 return report_progress(arg, path, NULL, status, NULL);
668 static const struct got_error *
669 patch_add(void *arg, unsigned char status, const char *path)
671 return report_progress(arg, NULL, path, status, NULL);
674 static const struct got_error *
675 open_blob(char **path, FILE **fp, const char *blobid,
676 struct got_repository *repo)
678 const struct got_error *err = NULL;
679 struct got_blob_object *blob = NULL;
680 struct got_object_id id, *idptr, *matched_id = NULL;
681 int fd = -1;
683 *fp = NULL;
684 *path = NULL;
686 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
687 err = got_repo_match_object_id(&matched_id, NULL, blobid,
688 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
689 repo);
690 if (err)
691 return err;
692 idptr = matched_id;
693 } else {
694 if (!got_parse_sha1_digest(id.sha1, blobid))
695 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
696 idptr = &id;
699 fd = got_opentempfd();
700 if (fd == -1) {
701 err = got_error_from_errno("got_opentempfd");
702 goto done;
705 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
706 if (err)
707 goto done;
709 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
710 if (err)
711 goto done;
713 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
714 if (err)
715 goto done;
717 done:
718 if (fd != -1 && close(fd) == -1 && err == NULL)
719 err = got_error_from_errno("close");
720 if (blob)
721 got_object_blob_close(blob);
722 if (matched_id != NULL)
723 free(matched_id);
724 if (err) {
725 if (*fp != NULL)
726 fclose(*fp);
727 if (*path != NULL)
728 unlink(*path);
729 free(*path);
730 *fp = NULL;
731 *path = NULL;
733 return err;
736 static const struct got_error *
737 prepare_merge(int *do_merge, char **apath, FILE **afile,
738 struct got_worktree *worktree, struct got_repository *repo,
739 struct got_patch *p, struct got_object_id *commit_id,
740 struct got_tree_object *tree, const char *path)
742 const struct got_error *err = NULL;
744 *do_merge = 0;
745 *apath = NULL;
746 *afile = NULL;
748 /* don't run the diff3 merge on creations/deletions */
749 if (p->old == NULL || p->new == NULL)
750 return NULL;
752 if (commit_id) {
753 struct got_object_id *id;
755 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
756 if (err)
757 return err;
758 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
759 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
760 free(id);
761 err = open_blob(apath, afile, p->blob, repo);
762 *do_merge = err == NULL;
763 } else if (*p->blob != '\0') {
764 err = open_blob(apath, afile, p->blob, repo);
765 /*
766 * ignore failures to open this blob, we might have
767 * parsed gibberish.
768 */
769 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
770 err->code != GOT_ERR_NO_OBJ)
771 return err;
772 *do_merge = err == NULL;
773 err = NULL;
776 return err;
779 static const struct got_error *
780 apply_patch(int *overlapcnt, struct got_worktree *worktree,
781 struct got_repository *repo, struct got_fileindex *fileindex,
782 const char *old, const char *new, struct got_patch *p, int nop,
783 int reverse, struct got_object_id *commit_id,
784 struct got_tree_object *tree, struct patch_args *pa,
785 got_cancel_cb cancel_cb, void *cancel_arg)
787 const struct got_error *err = NULL;
788 struct stat sb;
789 int do_merge = 0, file_renamed = 0;
790 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
791 char *oldpath = NULL, *newpath = NULL;
792 char *tmppath = NULL, *template = NULL;
793 char *apath = NULL, *mergepath = NULL;
794 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
795 int outfd;
796 mode_t mode = GOT_DEFAULT_FILE_MODE;
798 *overlapcnt = 0;
800 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
801 commit_id, tree, old);
802 if (err)
803 return err;
805 if (reverse && !do_merge)
806 reverse_patch(p);
808 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
809 old) == -1) {
810 err = got_error_from_errno("asprintf");
811 goto done;
814 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
815 new) == -1) {
816 err = got_error_from_errno("asprintf");
817 goto done;
820 file_renamed = strcmp(oldpath, newpath);
822 if (asprintf(&template, "%s/got-patch",
823 got_worktree_get_root_path(worktree)) == -1) {
824 err = got_error_from_errno(template);
825 goto done;
828 if (p->old != NULL) {
829 if ((oldfile = fopen(oldpath, "r")) == NULL) {
830 err = got_error_from_errno2("open", oldpath);
831 goto done;
833 if (fstat(fileno(oldfile), &sb) == -1) {
834 err = got_error_from_errno2("fstat", oldpath);
835 goto done;
837 mode = sb.st_mode;
838 } else if (p->xbit)
839 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
841 err = got_opentemp_named(&tmppath, &tmpfile, template);
842 if (err)
843 goto done;
844 outfd = fileno(tmpfile);
845 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
846 if (err)
847 goto done;
849 if (do_merge) {
850 const char *type, *id;
852 if (fseeko(afile, 0, SEEK_SET) == -1 ||
853 fseeko(oldfile, 0, SEEK_SET) == -1 ||
854 fseeko(tmpfile, 0, SEEK_SET) == -1) {
855 err = got_error_from_errno("fseeko");
856 goto done;
859 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
860 err = got_error_from_errno("asprintf");
861 oldlabel = NULL;
862 goto done;
865 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
866 err = got_error_from_errno("asprintf");
867 newlabel = NULL;
868 goto done;
871 if (*p->cid != '\0') {
872 type = "commit";
873 id = p->cid;
874 } else {
875 type = "blob";
876 id = p->blob;
879 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
880 err = got_error_from_errno("asprintf");
881 anclabel = NULL;
882 goto done;
885 if (reverse) {
886 char *s;
887 FILE *t;
889 s = anclabel;
890 anclabel = newlabel;
891 newlabel = s;
893 t = afile;
894 afile = tmpfile;
895 tmpfile = t;
898 err = got_opentemp_named(&mergepath, &mergefile, template);
899 if (err)
900 goto done;
901 outfd = fileno(mergefile);
903 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
904 oldfile, oldlabel, anclabel, newlabel,
905 GOT_DIFF_ALGORITHM_PATIENCE);
906 if (err)
907 goto done;
910 if (nop)
911 goto done;
913 if (p->old != NULL && p->new == NULL) {
914 err = got_worktree_patch_schedule_rm(old, repo, worktree,
915 fileindex, patch_delete, pa);
916 goto done;
919 if (fchmod(outfd, mode) == -1) {
920 err = got_error_from_errno2("chmod", tmppath);
921 goto done;
924 if (mergepath) {
925 err = got_path_move_file(mergepath, newpath);
926 if (err)
927 goto done;
928 free(mergepath);
929 mergepath = NULL;
930 } else {
931 err = got_path_move_file(tmppath, newpath);
932 if (err)
933 goto done;
934 free(tmppath);
935 tmppath = NULL;
938 if (file_renamed) {
939 err = got_worktree_patch_schedule_rm(old, repo, worktree,
940 fileindex, patch_delete, pa);
941 if (err == NULL)
942 err = got_worktree_patch_schedule_add(new, repo,
943 worktree, fileindex, patch_add,
944 pa);
945 if (err)
946 unlink(newpath);
947 } else if (p->old == NULL) {
948 err = got_worktree_patch_schedule_add(new, repo, worktree,
949 fileindex, patch_add, pa);
950 if (err)
951 unlink(newpath);
952 } else if (*overlapcnt != 0)
953 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
954 else if (do_merge)
955 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
956 else
957 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
959 done:
960 free(template);
962 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
963 err = got_error_from_errno("unlink");
964 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
965 err = got_error_from_errno("fclose");
966 free(tmppath);
968 free(oldpath);
969 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
970 err = got_error_from_errno("fclose");
972 if (apath != NULL && unlink(apath) == -1 && err == NULL)
973 err = got_error_from_errno("unlink");
974 if (afile != NULL && fclose(afile) == EOF && err == NULL)
975 err = got_error_from_errno("fclose");
976 free(apath);
978 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
979 err = got_error_from_errno("unlink");
980 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
981 err = got_error_from_errno("fclose");
982 free(mergepath);
984 free(newpath);
985 free(oldlabel);
986 free(newlabel);
987 free(anclabel);
988 return err;
991 const struct got_error *
992 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
993 int nop, int strip, int reverse, struct got_object_id *commit_id,
994 got_patch_progress_cb progress_cb, void *progress_arg,
995 got_cancel_cb cancel_cb, void *cancel_arg)
997 const struct got_error *err = NULL, *complete_err = NULL;
998 struct got_fileindex *fileindex = NULL;
999 struct got_commit_object *commit = NULL;
1000 struct got_tree_object *tree = NULL;
1001 char *fileindex_path = NULL;
1002 char *oldpath, *newpath;
1003 struct imsgbuf *ibuf;
1004 int imsg_fds[2] = {-1, -1};
1005 int overlapcnt, done = 0, failed = 0;
1006 pid_t pid;
1008 ibuf = calloc(1, sizeof(*ibuf));
1009 if (ibuf == NULL) {
1010 err = got_error_from_errno("calloc");
1011 goto done;
1014 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1015 err = got_error_from_errno("socketpair");
1016 goto done;
1019 pid = fork();
1020 if (pid == -1) {
1021 err = got_error_from_errno("fork");
1022 goto done;
1023 } else if (pid == 0) {
1024 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1025 NULL);
1026 /* not reached */
1029 if (close(imsg_fds[1]) == -1) {
1030 err = got_error_from_errno("close");
1031 goto done;
1033 imsg_fds[1] = -1;
1034 imsg_init(ibuf, imsg_fds[0]);
1036 err = send_patch(ibuf, fd);
1037 fd = -1;
1038 if (err)
1039 goto done;
1041 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1042 worktree);
1043 if (err)
1044 goto done;
1046 if (commit_id) {
1047 err = got_object_open_as_commit(&commit, repo, commit_id);
1048 if (err)
1049 goto done;
1051 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1052 if (err)
1053 goto done;
1056 while (!done && err == NULL) {
1057 struct got_patch p;
1058 struct patch_args pa;
1060 pa.progress_cb = progress_cb;
1061 pa.progress_arg = progress_arg;
1062 pa.head = &p.head;
1064 err = recv_patch(ibuf, &done, &p, strip);
1065 if (err || done)
1066 break;
1068 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1069 &newpath, worktree, repo, fileindex);
1070 if (err == NULL)
1071 err = apply_patch(&overlapcnt, worktree, repo,
1072 fileindex, oldpath, newpath, &p, nop, reverse,
1073 commit_id, tree, &pa, cancel_cb, cancel_arg);
1074 if (err != NULL) {
1075 failed = 1;
1076 /* recoverable errors */
1077 if (err->code == GOT_ERR_FILE_STATUS ||
1078 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1079 err = report_progress(&pa, p.old, p.new,
1080 GOT_STATUS_CANNOT_UPDATE, err);
1081 else if (err->code == GOT_ERR_HUNK_FAILED)
1082 err = report_progress(&pa, p.old, p.new,
1083 GOT_STATUS_CANNOT_UPDATE, NULL);
1085 if (overlapcnt != 0)
1086 failed = 1;
1088 free(oldpath);
1089 free(newpath);
1090 patch_free(&p);
1092 if (err)
1093 break;
1096 done:
1097 if (fileindex != NULL)
1098 complete_err = got_worktree_patch_complete(fileindex,
1099 fileindex_path);
1100 if (complete_err && err == NULL)
1101 err = complete_err;
1102 free(fileindex_path);
1103 if (tree)
1104 got_object_tree_close(tree);
1105 if (commit)
1106 got_object_commit_close(commit);
1107 if (fd != -1 && close(fd) == -1 && err == NULL)
1108 err = got_error_from_errno("close");
1109 if (ibuf != NULL)
1110 imsg_clear(ibuf);
1111 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1112 err = got_error_from_errno("close");
1113 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1114 err = got_error_from_errno("close");
1115 if (err == NULL && failed)
1116 err = got_error(GOT_ERR_PATCH_FAILED);
1117 return err;