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_hunk {
63 STAILQ_ENTRY(got_patch_hunk) entries;
64 const struct got_error *err;
65 int ws_mangled;
66 int offset;
67 int old_nonl;
68 int new_nonl;
69 int old_from;
70 int old_lines;
71 int new_from;
72 int new_lines;
73 size_t len;
74 size_t cap;
75 char **lines;
76 };
78 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
79 struct got_patch {
80 int xbit;
81 char *old;
82 char *new;
83 char cid[41];
84 char blob[41];
85 struct got_patch_hunk_head head;
86 };
88 struct patch_args {
89 got_patch_progress_cb progress_cb;
90 void *progress_arg;
91 struct got_patch_hunk_head *head;
92 };
94 static mode_t
95 apply_umask(mode_t mode)
96 {
97 mode_t um;
99 um = umask(000);
100 umask(um);
101 return mode & ~um;
104 static const struct got_error *
105 send_patch(struct imsgbuf *ibuf, int fd)
107 const struct got_error *err = NULL;
109 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
110 NULL, 0) == -1) {
111 err = got_error_from_errno(
112 "imsg_compose GOT_IMSG_PATCH_FILE");
113 close(fd);
114 return err;
117 return got_privsep_flush_imsg(ibuf);
120 static void
121 patch_free(struct got_patch *p)
123 struct got_patch_hunk *h;
124 size_t i;
126 while (!STAILQ_EMPTY(&p->head)) {
127 h = STAILQ_FIRST(&p->head);
128 STAILQ_REMOVE_HEAD(&p->head, entries);
130 for (i = 0; i < h->len; ++i)
131 free(h->lines[i]);
132 free(h->lines);
133 free(h);
136 free(p->new);
137 free(p->old);
139 memset(p, 0, sizeof(*p));
140 STAILQ_INIT(&p->head);
143 static const struct got_error *
144 pushline(struct got_patch_hunk *h, const char *line)
146 void *t;
147 size_t newcap;
149 if (h->len == h->cap) {
150 if ((newcap = h->cap * 1.5) == 0)
151 newcap = 16;
152 t = recallocarray(h->lines, h->cap, newcap,
153 sizeof(h->lines[0]));
154 if (t == NULL)
155 return got_error_from_errno("recallocarray");
156 h->lines = t;
157 h->cap = newcap;
160 if ((t = strdup(line)) == NULL)
161 return got_error_from_errno("strdup");
163 h->lines[h->len++] = t;
164 return NULL;
167 static const struct got_error *
168 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
170 const struct got_error *err = NULL;
171 struct imsg imsg;
172 struct got_imsg_patch_hunk hdr;
173 struct got_imsg_patch patch;
174 struct got_patch_hunk *h = NULL;
175 size_t datalen;
176 int lastmode = -1;
178 memset(p, 0, sizeof(*p));
179 STAILQ_INIT(&p->head);
181 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
182 if (err)
183 return err;
184 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
185 *done = 1;
186 goto done;
188 if (imsg.hdr.type != GOT_IMSG_PATCH) {
189 err = got_error(GOT_ERR_PRIVSEP_MSG);
190 goto done;
192 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
193 if (datalen != sizeof(patch)) {
194 err = got_error(GOT_ERR_PRIVSEP_LEN);
195 goto done;
197 memcpy(&patch, imsg.data, sizeof(patch));
199 if (patch.old[sizeof(patch.old)-1] != '\0' ||
200 patch.new[sizeof(patch.new)-1] != '\0' ||
201 patch.cid[sizeof(patch.cid)-1] != '\0' ||
202 patch.blob[sizeof(patch.blob)-1] != '\0') {
203 err = got_error(GOT_ERR_PRIVSEP_LEN);
204 goto done;
207 if (*patch.cid != '\0')
208 strlcpy(p->cid, patch.cid, sizeof(p->cid));
210 if (*patch.blob != '\0')
211 strlcpy(p->blob, patch.blob, sizeof(p->blob));
213 p->xbit = patch.xbit;
215 /* automatically set strip=1 for git-style diffs */
216 if (strip == -1 && patch.git &&
217 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
218 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
219 strip = 1;
221 /* prefer the new name if not /dev/null for not git-style diffs */
222 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
223 err = got_path_strip(&p->old, patch.new, strip);
224 if (err)
225 goto done;
226 } else if (*patch.old != '\0') {
227 err = got_path_strip(&p->old, patch.old, strip);
228 if (err)
229 goto done;
232 if (*patch.new != '\0') {
233 err = got_path_strip(&p->new, patch.new, strip);
234 if (err)
235 goto done;
238 if (p->old == NULL && p->new == NULL) {
239 err = got_error(GOT_ERR_PATCH_MALFORMED);
240 goto done;
243 imsg_free(&imsg);
245 for (;;) {
246 char *t;
248 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
249 if (err) {
250 patch_free(p);
251 return err;
254 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
255 switch (imsg.hdr.type) {
256 case GOT_IMSG_PATCH_DONE:
257 if (h != NULL && h->len == 0)
258 err = got_error(GOT_ERR_PATCH_MALFORMED);
259 goto done;
260 case GOT_IMSG_PATCH_HUNK:
261 if (h != NULL &&
262 (h->len == 0 || h->old_nonl || h->new_nonl)) {
263 err = got_error(GOT_ERR_PATCH_MALFORMED);
264 goto done;
266 lastmode = -1;
267 if (datalen != sizeof(hdr)) {
268 err = got_error(GOT_ERR_PRIVSEP_LEN);
269 goto done;
271 memcpy(&hdr, imsg.data, sizeof(hdr));
272 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
273 err = got_error(GOT_ERR_PRIVSEP_LEN);
274 goto done;
276 if ((h = calloc(1, sizeof(*h))) == NULL) {
277 err = got_error_from_errno("calloc");
278 goto done;
280 h->old_from = hdr.oldfrom;
281 h->old_lines = hdr.oldlines;
282 h->new_from = hdr.newfrom;
283 h->new_lines = hdr.newlines;
284 STAILQ_INSERT_TAIL(&p->head, h, entries);
285 break;
286 case GOT_IMSG_PATCH_LINE:
287 if (h == NULL) {
288 err = got_error(GOT_ERR_PRIVSEP_MSG);
289 goto done;
291 t = imsg.data;
292 /* at least one char */
293 if (datalen < 2 || t[datalen-1] != '\0') {
294 err = got_error(GOT_ERR_PRIVSEP_MSG);
295 goto done;
297 if (*t != ' ' && *t != '-' && *t != '+' &&
298 *t != '\\') {
299 err = got_error(GOT_ERR_PRIVSEP_MSG);
300 goto done;
303 if (*t != '\\')
304 err = pushline(h, t);
305 else if (lastmode == '-')
306 h->old_nonl = 1;
307 else if (lastmode == '+')
308 h->new_nonl = 1;
309 else
310 err = got_error(GOT_ERR_PATCH_MALFORMED);
312 if (err)
313 goto done;
315 lastmode = *t;
316 break;
317 default:
318 err = got_error(GOT_ERR_PRIVSEP_MSG);
319 goto done;
322 imsg_free(&imsg);
325 done:
326 if (err)
327 patch_free(p);
329 imsg_free(&imsg);
330 return err;
333 static void
334 reverse_patch(struct got_patch *p)
336 struct got_patch_hunk *h;
337 size_t i;
338 int tmp;
340 STAILQ_FOREACH(h, &p->head, entries) {
341 tmp = h->old_from;
342 h->old_from = h->new_from;
343 h->new_from = tmp;
345 tmp = h->old_lines;
346 h->old_lines = h->new_lines;
347 h->new_lines = tmp;
349 tmp = h->old_nonl;
350 h->old_nonl = h->new_nonl;
351 h->new_nonl = tmp;
353 for (i = 0; i < h->len; ++i) {
354 if (*h->lines[i] == '+')
355 *h->lines[i] = '-';
356 else if (*h->lines[i] == '-')
357 *h->lines[i] = '+';
362 /*
363 * Copy data from orig starting at copypos until pos into tmp.
364 * If pos is -1, copy until EOF.
365 */
366 static const struct got_error *
367 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
369 char buf[BUFSIZ];
370 size_t len, r, w;
372 if (fseeko(orig, copypos, SEEK_SET) == -1)
373 return got_error_from_errno("fseeko");
375 while (pos == -1 || copypos < pos) {
376 len = sizeof(buf);
377 if (pos > 0)
378 len = MIN(len, (size_t)pos - copypos);
379 r = fread(buf, 1, len, orig);
380 if (r != len && ferror(orig))
381 return got_error_from_errno("fread");
382 w = fwrite(buf, 1, r, tmp);
383 if (w != r)
384 return got_error_from_errno("fwrite");
385 copypos += len;
386 if (r != len && feof(orig)) {
387 if (pos == -1)
388 return NULL;
389 return got_error(GOT_ERR_HUNK_FAILED);
392 return NULL;
395 static int linecmp(const char *, const char *, int *);
397 static const struct got_error *
398 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
400 const struct got_error *err = NULL;
401 char *line = NULL;
402 char mode = *h->lines[0];
403 size_t linesize = 0;
404 ssize_t linelen;
405 off_t match = -1;
406 int mangled = 0, match_lineno = -1;
408 for (;;) {
409 linelen = getline(&line, &linesize, orig);
410 if (linelen == -1) {
411 if (ferror(orig))
412 err = got_error_from_errno("getline");
413 else if (match == -1)
414 err = got_error(GOT_ERR_HUNK_FAILED);
415 break;
417 if (line[linelen - 1] == '\n')
418 line[linelen - 1] = '\0';
419 (*lineno)++;
421 if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
422 (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
423 (mode == '+' && *lineno == h->old_from)) {
424 match = ftello(orig);
425 if (match == -1) {
426 err = got_error_from_errno("ftello");
427 break;
429 match -= linelen;
430 match_lineno = (*lineno)-1;
433 if (*lineno >= h->old_from && match != -1) {
434 if (mangled)
435 h->ws_mangled = 1;
436 break;
440 if (err == NULL) {
441 *pos = match;
442 *lineno = match_lineno;
443 if (fseeko(orig, match, SEEK_SET) == -1)
444 err = got_error_from_errno("fseeko");
447 free(line);
448 return err;
451 static int
452 linecmp(const char *a, const char *b, int *mangled)
454 int c;
456 *mangled = 0;
457 c = strcmp(a, b);
458 if (c == 0)
459 return c;
461 *mangled = 1;
462 for (;;) {
463 while (*a == '\t' || *a == ' ' || *a == '\f')
464 a++;
465 while (*b == '\t' || *b == ' ' || *b == '\f')
466 b++;
467 if (*a == '\0' || *a != *b)
468 break;
469 a++, b++;
472 return *a - *b;
475 static const struct got_error *
476 test_hunk(FILE *orig, struct got_patch_hunk *h)
478 const struct got_error *err = NULL;
479 char *line = NULL;
480 size_t linesize = 0, i = 0;
481 ssize_t linelen;
482 int mangled;
484 for (i = 0; i < h->len; ++i) {
485 switch (*h->lines[i]) {
486 case '+':
487 continue;
488 case ' ':
489 case '-':
490 linelen = getline(&line, &linesize, orig);
491 if (linelen == -1) {
492 if (ferror(orig))
493 err = got_error_from_errno("getline");
494 else
495 err = got_error(
496 GOT_ERR_HUNK_FAILED);
497 goto done;
499 if (line[linelen - 1] == '\n')
500 line[linelen - 1] = '\0';
501 if (linecmp(h->lines[i] + 1, line, &mangled)) {
502 err = got_error(GOT_ERR_HUNK_FAILED);
503 goto done;
505 if (mangled)
506 h->ws_mangled = 1;
507 break;
511 done:
512 free(line);
513 return err;
516 static const struct got_error *
517 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
518 off_t from)
520 const struct got_error *err = NULL;
521 const char *t;
522 size_t linesize = 0, i, new = 0;
523 char *line = NULL;
524 char mode;
525 ssize_t linelen;
527 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
528 return got_error_from_errno("fseeko");
530 for (i = 0; i < h->len; ++i) {
531 switch (mode = *h->lines[i]) {
532 case '-':
533 case ' ':
534 (*lineno)++;
535 if (orig != NULL) {
536 linelen = getline(&line, &linesize, orig);
537 if (linelen == -1) {
538 err = got_error_from_errno("getline");
539 goto done;
541 if (line[linelen - 1] == '\n')
542 line[linelen - 1] = '\0';
543 t = line;
544 } else
545 t = h->lines[i] + 1;
546 if (mode == '-')
547 continue;
548 if (fprintf(tmp, "%s\n", t) < 0) {
549 err = got_error_from_errno("fprintf");
550 goto done;
552 break;
553 case '+':
554 new++;
555 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
556 err = got_error_from_errno("fprintf");
557 goto done;
559 if (new != h->new_lines || !h->new_nonl) {
560 if (fprintf(tmp, "\n") < 0) {
561 err = got_error_from_errno("fprintf");
562 goto done;
565 break;
569 done:
570 free(line);
571 return err;
574 static const struct got_error *
575 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
577 const struct got_error *err = NULL;
578 struct got_patch_hunk *h;
579 struct stat sb;
580 int lineno = 0;
581 off_t copypos, pos;
582 char *line = NULL;
583 size_t linesize = 0;
584 ssize_t linelen;
586 if (p->old == NULL) { /* create */
587 h = STAILQ_FIRST(&p->head);
588 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
589 return got_error(GOT_ERR_PATCH_MALFORMED);
590 return apply_hunk(orig, tmp, h, &lineno, 0);
593 /* When deleting binary files there are no hunks to apply. */
594 if (p->new == NULL && STAILQ_EMPTY(&p->head))
595 return NULL;
597 if (fstat(fileno(orig), &sb) == -1)
598 return got_error_from_errno("fstat");
600 copypos = 0;
601 STAILQ_FOREACH(h, &p->head, entries) {
602 tryagain:
603 err = locate_hunk(orig, h, &pos, &lineno);
604 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
605 h->err = err;
606 if (err != NULL)
607 return err;
608 err = copy(tmp, orig, copypos, pos);
609 if (err != NULL)
610 return err;
611 copypos = pos;
613 err = test_hunk(orig, h);
614 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
615 /*
616 * try to apply the hunk again starting the search
617 * after the previous partial match.
618 */
619 if (fseeko(orig, pos, SEEK_SET) == -1)
620 return got_error_from_errno("fseeko");
621 linelen = getline(&line, &linesize, orig);
622 if (linelen == -1)
623 return got_error_from_errno("getline");
624 lineno++;
625 goto tryagain;
627 if (err != NULL)
628 return err;
630 if (lineno + 1 != h->old_from)
631 h->offset = lineno + 1 - h->old_from;
633 err = apply_hunk(orig, tmp, h, &lineno, pos);
634 if (err != NULL)
635 return err;
637 copypos = ftello(orig);
638 if (copypos == -1)
639 return got_error_from_errno("ftello");
642 if (p->new == NULL && sb.st_size != copypos) {
643 h = STAILQ_FIRST(&p->head);
644 h->err = got_error(GOT_ERR_HUNK_FAILED);
645 err = h->err;
646 } else if (!feof(orig))
647 err = copy(tmp, orig, copypos, -1);
649 return err;
652 static const struct got_error *
653 report_progress(struct patch_args *pa, const char *old, const char *new,
654 unsigned char status, const struct got_error *orig_error)
656 const struct got_error *err;
657 struct got_patch_hunk *h;
659 err = pa->progress_cb(pa->progress_arg, old, new, status,
660 orig_error, 0, 0, 0, 0, 0, 0, NULL);
661 if (err)
662 return err;
664 STAILQ_FOREACH(h, pa->head, entries) {
665 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
666 continue;
668 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
669 h->old_from, h->old_lines, h->new_from, h->new_lines,
670 h->offset, h->ws_mangled, h->err);
671 if (err)
672 return err;
675 return NULL;
678 static const struct got_error *
679 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
680 const char *path)
682 return report_progress(arg, path, NULL, status, NULL);
685 static const struct got_error *
686 patch_add(void *arg, unsigned char status, const char *path)
688 return report_progress(arg, NULL, path, status, NULL);
691 static const struct got_error *
692 open_blob(char **path, FILE **fp, const char *blobid,
693 struct got_repository *repo)
695 const struct got_error *err = NULL;
696 struct got_blob_object *blob = NULL;
697 struct got_object_id id, *idptr, *matched_id = NULL;
698 int fd = -1;
700 *fp = NULL;
701 *path = NULL;
703 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
704 err = got_repo_match_object_id(&matched_id, NULL, blobid,
705 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
706 repo);
707 if (err)
708 return err;
709 idptr = matched_id;
710 } else {
711 if (!got_parse_object_id(&id, blobid, GOT_HASH_SHA1))
712 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
713 idptr = &id;
716 fd = got_opentempfd();
717 if (fd == -1) {
718 err = got_error_from_errno("got_opentempfd");
719 goto done;
722 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
723 if (err)
724 goto done;
726 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
727 "");
728 if (err)
729 goto done;
731 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
732 if (err)
733 goto done;
735 done:
736 if (fd != -1 && close(fd) == -1 && err == NULL)
737 err = got_error_from_errno("close");
738 if (blob)
739 got_object_blob_close(blob);
740 if (matched_id != NULL)
741 free(matched_id);
742 if (err) {
743 if (*fp != NULL)
744 fclose(*fp);
745 if (*path != NULL)
746 unlink(*path);
747 free(*path);
748 *fp = NULL;
749 *path = NULL;
751 return err;
754 static const struct got_error *
755 prepare_merge(int *do_merge, char **apath, FILE **afile,
756 struct got_worktree *worktree, struct got_repository *repo,
757 struct got_patch *p, struct got_object_id *commit_id,
758 struct got_tree_object *tree, const char *path)
760 const struct got_error *err = NULL;
762 *do_merge = 0;
763 *apath = NULL;
764 *afile = NULL;
766 /* don't run the diff3 merge on creations/deletions */
767 if (p->old == NULL || p->new == NULL)
768 return NULL;
770 if (commit_id) {
771 struct got_object_id *id;
773 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
774 if (err)
775 return err;
776 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
777 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
778 free(id);
779 err = open_blob(apath, afile, p->blob, repo);
780 *do_merge = err == NULL;
781 } else if (*p->blob != '\0') {
782 err = open_blob(apath, afile, p->blob, repo);
783 /*
784 * ignore failures to open this blob, we might have
785 * parsed gibberish.
786 */
787 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
788 err->code != GOT_ERR_NO_OBJ)
789 return err;
790 *do_merge = err == NULL;
791 err = NULL;
794 return err;
797 static const struct got_error *
798 apply_patch(int *overlapcnt, struct got_worktree *worktree,
799 struct got_repository *repo, struct got_fileindex *fileindex,
800 const char *old, const char *new, struct got_patch *p, int nop,
801 int reverse, struct got_object_id *commit_id,
802 struct got_tree_object *tree, struct patch_args *pa,
803 got_cancel_cb cancel_cb, void *cancel_arg)
805 const struct got_error *err = NULL;
806 struct stat sb;
807 int do_merge = 0, file_renamed = 0;
808 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
809 char *oldpath = NULL, *newpath = NULL;
810 char *tmppath = NULL, *template = NULL;
811 char *apath = NULL, *mergepath = NULL;
812 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
813 int outfd;
814 mode_t mode = GOT_DEFAULT_FILE_MODE;
816 *overlapcnt = 0;
818 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
819 commit_id, tree, old);
820 if (err)
821 return err;
823 if (reverse && !do_merge)
824 reverse_patch(p);
826 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
827 old) == -1) {
828 err = got_error_from_errno("asprintf");
829 goto done;
832 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
833 new) == -1) {
834 err = got_error_from_errno("asprintf");
835 goto done;
838 file_renamed = strcmp(oldpath, newpath);
840 if (asprintf(&template, "%s/got-patch",
841 got_worktree_get_root_path(worktree)) == -1) {
842 err = got_error_from_errno(template);
843 goto done;
846 if (p->old != NULL) {
847 if ((oldfile = fopen(oldpath, "r")) == NULL) {
848 err = got_error_from_errno2("open", oldpath);
849 goto done;
851 if (fstat(fileno(oldfile), &sb) == -1) {
852 err = got_error_from_errno2("fstat", oldpath);
853 goto done;
855 mode = sb.st_mode;
856 } else if (p->xbit)
857 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
859 err = got_opentemp_named(&tmppath, &tmpfile, template, "");
860 if (err)
861 goto done;
862 outfd = fileno(tmpfile);
863 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
864 if (err)
865 goto done;
867 if (do_merge) {
868 const char *type, *id;
870 if (fseeko(afile, 0, SEEK_SET) == -1 ||
871 fseeko(oldfile, 0, SEEK_SET) == -1 ||
872 fseeko(tmpfile, 0, SEEK_SET) == -1) {
873 err = got_error_from_errno("fseeko");
874 goto done;
877 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
878 err = got_error_from_errno("asprintf");
879 oldlabel = NULL;
880 goto done;
883 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
884 err = got_error_from_errno("asprintf");
885 newlabel = NULL;
886 goto done;
889 if (*p->cid != '\0') {
890 type = "commit";
891 id = p->cid;
892 } else {
893 type = "blob";
894 id = p->blob;
897 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
898 err = got_error_from_errno("asprintf");
899 anclabel = NULL;
900 goto done;
903 if (reverse) {
904 char *s;
905 FILE *t;
907 s = anclabel;
908 anclabel = newlabel;
909 newlabel = s;
911 t = afile;
912 afile = tmpfile;
913 tmpfile = t;
916 err = got_opentemp_named(&mergepath, &mergefile, template, "");
917 if (err)
918 goto done;
919 outfd = fileno(mergefile);
921 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
922 oldfile, oldlabel, anclabel, newlabel,
923 GOT_DIFF_ALGORITHM_PATIENCE);
924 if (err)
925 goto done;
928 if (nop)
929 goto done;
931 if (p->old != NULL && p->new == NULL) {
932 err = got_worktree_patch_schedule_rm(old, repo, worktree,
933 fileindex, patch_delete, pa);
934 goto done;
937 if (fchmod(outfd, apply_umask(mode)) == -1) {
938 err = got_error_from_errno2("chmod", tmppath);
939 goto done;
942 if (mergepath) {
943 err = got_path_move_file(mergepath, newpath);
944 if (err)
945 goto done;
946 free(mergepath);
947 mergepath = NULL;
948 } else {
949 err = got_path_move_file(tmppath, newpath);
950 if (err)
951 goto done;
952 free(tmppath);
953 tmppath = NULL;
956 if (file_renamed) {
957 err = got_worktree_patch_schedule_rm(old, repo, worktree,
958 fileindex, patch_delete, pa);
959 if (err == NULL)
960 err = got_worktree_patch_schedule_add(new, repo,
961 worktree, fileindex, patch_add,
962 pa);
963 if (err)
964 unlink(newpath);
965 } else if (p->old == NULL) {
966 err = got_worktree_patch_schedule_add(new, repo, worktree,
967 fileindex, patch_add, pa);
968 if (err)
969 unlink(newpath);
970 } else if (*overlapcnt != 0)
971 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
972 else if (do_merge)
973 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
974 else
975 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
977 done:
978 free(template);
980 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
981 err = got_error_from_errno("unlink");
982 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
983 err = got_error_from_errno("fclose");
984 free(tmppath);
986 free(oldpath);
987 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
988 err = got_error_from_errno("fclose");
990 if (apath != NULL && unlink(apath) == -1 && err == NULL)
991 err = got_error_from_errno("unlink");
992 if (afile != NULL && fclose(afile) == EOF && err == NULL)
993 err = got_error_from_errno("fclose");
994 free(apath);
996 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
997 err = got_error_from_errno("unlink");
998 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
999 err = got_error_from_errno("fclose");
1000 free(mergepath);
1002 free(newpath);
1003 free(oldlabel);
1004 free(newlabel);
1005 free(anclabel);
1006 return err;
1009 const struct got_error *
1010 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1011 int nop, int strip, int reverse, struct got_object_id *commit_id,
1012 got_patch_progress_cb progress_cb, void *progress_arg,
1013 got_cancel_cb cancel_cb, void *cancel_arg)
1015 const struct got_error *err = NULL, *complete_err = NULL;
1016 struct got_fileindex *fileindex = NULL;
1017 struct got_commit_object *commit = NULL;
1018 struct got_tree_object *tree = NULL;
1019 char *fileindex_path = NULL;
1020 char *oldpath, *newpath;
1021 struct imsgbuf *ibuf;
1022 int imsg_fds[2] = {-1, -1};
1023 int overlapcnt, done = 0, failed = 0;
1024 pid_t pid;
1026 ibuf = calloc(1, sizeof(*ibuf));
1027 if (ibuf == NULL) {
1028 err = got_error_from_errno("calloc");
1029 goto done;
1032 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1033 err = got_error_from_errno("socketpair");
1034 goto done;
1037 pid = fork();
1038 if (pid == -1) {
1039 err = got_error_from_errno("fork");
1040 goto done;
1041 } else if (pid == 0) {
1042 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1043 NULL);
1044 /* not reached */
1047 if (close(imsg_fds[1]) == -1) {
1048 err = got_error_from_errno("close");
1049 goto done;
1051 imsg_fds[1] = -1;
1052 imsg_init(ibuf, imsg_fds[0]);
1054 err = send_patch(ibuf, fd);
1055 fd = -1;
1056 if (err)
1057 goto done;
1059 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1060 worktree);
1061 if (err)
1062 goto done;
1064 if (commit_id) {
1065 err = got_object_open_as_commit(&commit, repo, commit_id);
1066 if (err)
1067 goto done;
1069 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1070 if (err)
1071 goto done;
1074 while (!done && err == NULL) {
1075 struct got_patch p;
1076 struct patch_args pa;
1078 pa.progress_cb = progress_cb;
1079 pa.progress_arg = progress_arg;
1080 pa.head = &p.head;
1082 err = recv_patch(ibuf, &done, &p, strip);
1083 if (err || done)
1084 break;
1086 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1087 &newpath, worktree, repo, fileindex);
1088 if (err == NULL)
1089 err = apply_patch(&overlapcnt, worktree, repo,
1090 fileindex, oldpath, newpath, &p, nop, reverse,
1091 commit_id, tree, &pa, cancel_cb, cancel_arg);
1092 if (err != NULL) {
1093 failed = 1;
1094 /* recoverable errors */
1095 if (err->code == GOT_ERR_FILE_STATUS ||
1096 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1097 err = report_progress(&pa, p.old, p.new,
1098 GOT_STATUS_CANNOT_UPDATE, err);
1099 else if (err->code == GOT_ERR_HUNK_FAILED)
1100 err = report_progress(&pa, p.old, p.new,
1101 GOT_STATUS_CANNOT_UPDATE, NULL);
1103 if (overlapcnt != 0)
1104 failed = 1;
1106 free(oldpath);
1107 free(newpath);
1108 patch_free(&p);
1110 if (err)
1111 break;
1114 done:
1115 if (fileindex != NULL)
1116 complete_err = got_worktree_patch_complete(fileindex,
1117 fileindex_path);
1118 if (complete_err && err == NULL)
1119 err = complete_err;
1120 free(fileindex_path);
1121 if (tree)
1122 got_object_tree_close(tree);
1123 if (commit)
1124 got_object_commit_close(commit);
1125 if (fd != -1 && close(fd) == -1 && err == NULL)
1126 err = got_error_from_errno("close");
1127 if (ibuf != NULL)
1128 imsg_clear(ibuf);
1129 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1130 err = got_error_from_errno("close");
1131 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1132 err = got_error_from_errno("close");
1133 if (err == NULL && failed)
1134 err = got_error(GOT_ERR_PATCH_FAILED);
1135 return err;