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 #define MIN(a, b) ((a) < (b) ? (a) : (b))
60 struct got_patch_hunk {
61 STAILQ_ENTRY(got_patch_hunk) entries;
62 const struct got_error *err;
63 int ws_mangled;
64 int offset;
65 int old_nonl;
66 int new_nonl;
67 int old_from;
68 int old_lines;
69 int new_from;
70 int new_lines;
71 size_t len;
72 size_t cap;
73 char **lines;
74 };
76 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
77 struct got_patch {
78 int xbit;
79 char *old;
80 char *new;
81 char cid[41];
82 char blob[41];
83 struct got_patch_hunk_head head;
84 };
86 struct patch_args {
87 got_patch_progress_cb progress_cb;
88 void *progress_arg;
89 struct got_patch_hunk_head *head;
90 };
92 static mode_t
93 apply_umask(mode_t mode)
94 {
95 mode_t um;
97 um = umask(000);
98 umask(um);
99 return mode & ~um;
102 static const struct got_error *
103 send_patch(struct imsgbuf *ibuf, int fd)
105 const struct got_error *err = NULL;
107 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
108 NULL, 0) == -1) {
109 err = got_error_from_errno(
110 "imsg_compose GOT_IMSG_PATCH_FILE");
111 close(fd);
112 return err;
115 return got_privsep_flush_imsg(ibuf);
118 static void
119 patch_free(struct got_patch *p)
121 struct got_patch_hunk *h;
122 size_t i;
124 while (!STAILQ_EMPTY(&p->head)) {
125 h = STAILQ_FIRST(&p->head);
126 STAILQ_REMOVE_HEAD(&p->head, entries);
128 for (i = 0; i < h->len; ++i)
129 free(h->lines[i]);
130 free(h->lines);
131 free(h);
134 free(p->new);
135 free(p->old);
137 memset(p, 0, sizeof(*p));
138 STAILQ_INIT(&p->head);
141 static const struct got_error *
142 pushline(struct got_patch_hunk *h, const char *line)
144 void *t;
145 size_t newcap;
147 if (h->len == h->cap) {
148 if ((newcap = h->cap * 1.5) == 0)
149 newcap = 16;
150 t = recallocarray(h->lines, h->cap, newcap,
151 sizeof(h->lines[0]));
152 if (t == NULL)
153 return got_error_from_errno("recallocarray");
154 h->lines = t;
155 h->cap = newcap;
158 if ((t = strdup(line)) == NULL)
159 return got_error_from_errno("strdup");
161 h->lines[h->len++] = t;
162 return NULL;
165 static const struct got_error *
166 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
168 const struct got_error *err = NULL;
169 struct imsg imsg;
170 struct got_imsg_patch_hunk hdr;
171 struct got_imsg_patch patch;
172 struct got_patch_hunk *h = NULL;
173 size_t datalen;
174 int lastmode = -1;
176 memset(p, 0, sizeof(*p));
177 STAILQ_INIT(&p->head);
179 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
180 if (err)
181 return err;
182 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
183 *done = 1;
184 goto done;
186 if (imsg.hdr.type != GOT_IMSG_PATCH) {
187 err = got_error(GOT_ERR_PRIVSEP_MSG);
188 goto done;
190 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
191 if (datalen != sizeof(patch)) {
192 err = got_error(GOT_ERR_PRIVSEP_LEN);
193 goto done;
195 memcpy(&patch, imsg.data, sizeof(patch));
197 if (patch.old[sizeof(patch.old)-1] != '\0' ||
198 patch.new[sizeof(patch.new)-1] != '\0' ||
199 patch.cid[sizeof(patch.cid)-1] != '\0' ||
200 patch.blob[sizeof(patch.blob)-1] != '\0') {
201 err = got_error(GOT_ERR_PRIVSEP_LEN);
202 goto done;
205 if (*patch.cid != '\0')
206 strlcpy(p->cid, patch.cid, sizeof(p->cid));
208 if (*patch.blob != '\0')
209 strlcpy(p->blob, patch.blob, sizeof(p->blob));
211 p->xbit = patch.xbit;
213 /* automatically set strip=1 for git-style diffs */
214 if (strip == -1 && patch.git &&
215 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
216 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
217 strip = 1;
219 /* prefer the new name if not /dev/null for not git-style diffs */
220 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
221 err = got_path_strip(&p->old, patch.new, strip);
222 if (err)
223 goto done;
224 } else if (*patch.old != '\0') {
225 err = got_path_strip(&p->old, patch.old, strip);
226 if (err)
227 goto done;
230 if (*patch.new != '\0') {
231 err = got_path_strip(&p->new, patch.new, strip);
232 if (err)
233 goto done;
236 if (p->old == NULL && p->new == NULL) {
237 err = got_error(GOT_ERR_PATCH_MALFORMED);
238 goto done;
241 imsg_free(&imsg);
243 for (;;) {
244 char *t;
246 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
247 if (err) {
248 patch_free(p);
249 return err;
252 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
253 switch (imsg.hdr.type) {
254 case GOT_IMSG_PATCH_DONE:
255 if (h != NULL && h->len == 0)
256 err = got_error(GOT_ERR_PATCH_MALFORMED);
257 goto done;
258 case GOT_IMSG_PATCH_HUNK:
259 if (h != NULL &&
260 (h->len == 0 || h->old_nonl || h->new_nonl)) {
261 err = got_error(GOT_ERR_PATCH_MALFORMED);
262 goto done;
264 lastmode = -1;
265 if (datalen != sizeof(hdr)) {
266 err = got_error(GOT_ERR_PRIVSEP_LEN);
267 goto done;
269 memcpy(&hdr, imsg.data, sizeof(hdr));
270 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
271 err = got_error(GOT_ERR_PRIVSEP_LEN);
272 goto done;
274 if ((h = calloc(1, sizeof(*h))) == NULL) {
275 err = got_error_from_errno("calloc");
276 goto done;
278 h->old_from = hdr.oldfrom;
279 h->old_lines = hdr.oldlines;
280 h->new_from = hdr.newfrom;
281 h->new_lines = hdr.newlines;
282 STAILQ_INSERT_TAIL(&p->head, h, entries);
283 break;
284 case GOT_IMSG_PATCH_LINE:
285 if (h == NULL) {
286 err = got_error(GOT_ERR_PRIVSEP_MSG);
287 goto done;
289 t = imsg.data;
290 /* at least one char */
291 if (datalen < 2 || t[datalen-1] != '\0') {
292 err = got_error(GOT_ERR_PRIVSEP_MSG);
293 goto done;
295 if (*t != ' ' && *t != '-' && *t != '+' &&
296 *t != '\\') {
297 err = got_error(GOT_ERR_PRIVSEP_MSG);
298 goto done;
301 if (*t != '\\')
302 err = pushline(h, t);
303 else if (lastmode == '-')
304 h->old_nonl = 1;
305 else if (lastmode == '+')
306 h->new_nonl = 1;
307 else
308 err = got_error(GOT_ERR_PATCH_MALFORMED);
310 if (err)
311 goto done;
313 lastmode = *t;
314 break;
315 default:
316 err = got_error(GOT_ERR_PRIVSEP_MSG);
317 goto done;
320 imsg_free(&imsg);
323 done:
324 if (err)
325 patch_free(p);
327 imsg_free(&imsg);
328 return err;
331 static void
332 reverse_patch(struct got_patch *p)
334 struct got_patch_hunk *h;
335 size_t i;
336 int tmp;
338 STAILQ_FOREACH(h, &p->head, entries) {
339 tmp = h->old_from;
340 h->old_from = h->new_from;
341 h->new_from = tmp;
343 tmp = h->old_lines;
344 h->old_lines = h->new_lines;
345 h->new_lines = tmp;
347 tmp = h->old_nonl;
348 h->old_nonl = h->new_nonl;
349 h->new_nonl = tmp;
351 for (i = 0; i < h->len; ++i) {
352 if (*h->lines[i] == '+')
353 *h->lines[i] = '-';
354 else if (*h->lines[i] == '-')
355 *h->lines[i] = '+';
360 /*
361 * Copy data from orig starting at copypos until pos into tmp.
362 * If pos is -1, copy until EOF.
363 */
364 static const struct got_error *
365 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
367 char buf[BUFSIZ];
368 size_t len, r, w;
370 if (fseeko(orig, copypos, SEEK_SET) == -1)
371 return got_error_from_errno("fseeko");
373 while (pos == -1 || copypos < pos) {
374 len = sizeof(buf);
375 if (pos > 0)
376 len = MIN(len, (size_t)pos - copypos);
377 r = fread(buf, 1, len, orig);
378 if (r != len && ferror(orig))
379 return got_error_from_errno("fread");
380 w = fwrite(buf, 1, r, tmp);
381 if (w != r)
382 return got_error_from_errno("fwrite");
383 copypos += len;
384 if (r != len && feof(orig)) {
385 if (pos == -1)
386 return NULL;
387 return got_error(GOT_ERR_HUNK_FAILED);
390 return NULL;
393 static int linecmp(const char *, const char *, int *);
395 static const struct got_error *
396 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
398 const struct got_error *err = NULL;
399 char *line = NULL;
400 char mode = *h->lines[0];
401 size_t linesize = 0;
402 ssize_t linelen;
403 off_t match = -1;
404 int mangled = 0, match_lineno = -1;
406 for (;;) {
407 linelen = getline(&line, &linesize, orig);
408 if (linelen == -1) {
409 if (ferror(orig))
410 err = got_error_from_errno("getline");
411 else if (match == -1)
412 err = got_error(GOT_ERR_HUNK_FAILED);
413 break;
415 if (line[linelen - 1] == '\n')
416 line[linelen - 1] = '\0';
417 (*lineno)++;
419 if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
420 (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
421 (mode == '+' && *lineno == h->old_from)) {
422 match = ftello(orig);
423 if (match == -1) {
424 err = got_error_from_errno("ftello");
425 break;
427 match -= linelen;
428 match_lineno = (*lineno)-1;
431 if (*lineno >= h->old_from && match != -1) {
432 if (mangled)
433 h->ws_mangled = 1;
434 break;
438 if (err == NULL) {
439 *pos = match;
440 *lineno = match_lineno;
441 if (fseeko(orig, match, SEEK_SET) == -1)
442 err = got_error_from_errno("fseeko");
445 free(line);
446 return err;
449 static int
450 linecmp(const char *a, const char *b, int *mangled)
452 int c;
454 *mangled = 0;
455 c = strcmp(a, b);
456 if (c == 0)
457 return c;
459 *mangled = 1;
460 for (;;) {
461 while (*a == '\t' || *a == ' ' || *a == '\f')
462 a++;
463 while (*b == '\t' || *b == ' ' || *b == '\f')
464 b++;
465 if (*a == '\0' || *a != *b)
466 break;
467 a++, b++;
470 return *a - *b;
473 static const struct got_error *
474 test_hunk(FILE *orig, struct got_patch_hunk *h)
476 const struct got_error *err = NULL;
477 char *line = NULL;
478 size_t linesize = 0, i = 0;
479 ssize_t linelen;
480 int mangled;
482 for (i = 0; i < h->len; ++i) {
483 switch (*h->lines[i]) {
484 case '+':
485 continue;
486 case ' ':
487 case '-':
488 linelen = getline(&line, &linesize, orig);
489 if (linelen == -1) {
490 if (ferror(orig))
491 err = got_error_from_errno("getline");
492 else
493 err = got_error(
494 GOT_ERR_HUNK_FAILED);
495 goto done;
497 if (line[linelen - 1] == '\n')
498 line[linelen - 1] = '\0';
499 if (linecmp(h->lines[i] + 1, line, &mangled)) {
500 err = got_error(GOT_ERR_HUNK_FAILED);
501 goto done;
503 if (mangled)
504 h->ws_mangled = 1;
505 break;
509 done:
510 free(line);
511 return err;
514 static const struct got_error *
515 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
516 off_t from)
518 const struct got_error *err = NULL;
519 const char *t;
520 size_t linesize = 0, i, new = 0;
521 char *line = NULL;
522 char mode;
523 ssize_t linelen;
525 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
526 return got_error_from_errno("fseeko");
528 for (i = 0; i < h->len; ++i) {
529 switch (mode = *h->lines[i]) {
530 case '-':
531 case ' ':
532 (*lineno)++;
533 if (orig != NULL) {
534 linelen = getline(&line, &linesize, orig);
535 if (linelen == -1) {
536 err = got_error_from_errno("getline");
537 goto done;
539 if (line[linelen - 1] == '\n')
540 line[linelen - 1] = '\0';
541 t = line;
542 } else
543 t = h->lines[i] + 1;
544 if (mode == '-')
545 continue;
546 if (fprintf(tmp, "%s\n", t) < 0) {
547 err = got_error_from_errno("fprintf");
548 goto done;
550 break;
551 case '+':
552 new++;
553 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
554 err = got_error_from_errno("fprintf");
555 goto done;
557 if (new != h->new_lines || !h->new_nonl) {
558 if (fprintf(tmp, "\n") < 0) {
559 err = got_error_from_errno("fprintf");
560 goto done;
563 break;
567 done:
568 free(line);
569 return err;
572 static const struct got_error *
573 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
575 const struct got_error *err = NULL;
576 struct got_patch_hunk *h;
577 struct stat sb;
578 int lineno = 0;
579 off_t copypos, pos;
580 char *line = NULL;
581 size_t linesize = 0;
582 ssize_t linelen;
584 if (p->old == NULL) { /* create */
585 h = STAILQ_FIRST(&p->head);
586 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
587 return got_error(GOT_ERR_PATCH_MALFORMED);
588 return apply_hunk(orig, tmp, h, &lineno, 0);
591 /* When deleting binary files there are no hunks to apply. */
592 if (p->new == NULL && STAILQ_EMPTY(&p->head))
593 return NULL;
595 if (fstat(fileno(orig), &sb) == -1)
596 return got_error_from_errno("fstat");
598 copypos = 0;
599 STAILQ_FOREACH(h, &p->head, entries) {
600 tryagain:
601 err = locate_hunk(orig, h, &pos, &lineno);
602 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
603 h->err = err;
604 if (err != NULL)
605 return err;
606 err = copy(tmp, orig, copypos, pos);
607 if (err != NULL)
608 return err;
609 copypos = pos;
611 err = test_hunk(orig, h);
612 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
613 /*
614 * try to apply the hunk again starting the search
615 * after the previous partial match.
616 */
617 if (fseeko(orig, pos, SEEK_SET) == -1)
618 return got_error_from_errno("fseeko");
619 linelen = getline(&line, &linesize, orig);
620 if (linelen == -1)
621 return got_error_from_errno("getline");
622 lineno++;
623 goto tryagain;
625 if (err != NULL)
626 return err;
628 if (lineno + 1 != h->old_from)
629 h->offset = lineno + 1 - h->old_from;
631 err = apply_hunk(orig, tmp, h, &lineno, pos);
632 if (err != NULL)
633 return err;
635 copypos = ftello(orig);
636 if (copypos == -1)
637 return got_error_from_errno("ftello");
640 if (p->new == NULL && sb.st_size != copypos) {
641 h = STAILQ_FIRST(&p->head);
642 h->err = got_error(GOT_ERR_HUNK_FAILED);
643 err = h->err;
644 } else if (!feof(orig))
645 err = copy(tmp, orig, copypos, -1);
647 return err;
650 static const struct got_error *
651 report_progress(struct patch_args *pa, const char *old, const char *new,
652 unsigned char status, const struct got_error *orig_error)
654 const struct got_error *err;
655 struct got_patch_hunk *h;
657 err = pa->progress_cb(pa->progress_arg, old, new, status,
658 orig_error, 0, 0, 0, 0, 0, 0, NULL);
659 if (err)
660 return err;
662 STAILQ_FOREACH(h, pa->head, entries) {
663 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
664 continue;
666 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
667 h->old_from, h->old_lines, h->new_from, h->new_lines,
668 h->offset, h->ws_mangled, h->err);
669 if (err)
670 return err;
673 return NULL;
676 static const struct got_error *
677 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
678 const char *path)
680 return report_progress(arg, path, NULL, status, NULL);
683 static const struct got_error *
684 patch_add(void *arg, unsigned char status, const char *path)
686 return report_progress(arg, NULL, path, status, NULL);
689 static const struct got_error *
690 open_blob(char **path, FILE **fp, const char *blobid,
691 struct got_repository *repo)
693 const struct got_error *err = NULL;
694 struct got_blob_object *blob = NULL;
695 struct got_object_id id, *idptr, *matched_id = NULL;
696 int fd = -1;
698 *fp = NULL;
699 *path = NULL;
701 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
702 err = got_repo_match_object_id(&matched_id, NULL, blobid,
703 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
704 repo);
705 if (err)
706 return err;
707 idptr = matched_id;
708 } else {
709 memset(&id, 0, sizeof(id));
710 if (!got_parse_sha1_digest(id.hash, blobid))
711 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
712 idptr = &id;
715 fd = got_opentempfd();
716 if (fd == -1) {
717 err = got_error_from_errno("got_opentempfd");
718 goto done;
721 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
722 if (err)
723 goto done;
725 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
726 "");
727 if (err)
728 goto done;
730 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
731 if (err)
732 goto done;
734 done:
735 if (fd != -1 && close(fd) == -1 && err == NULL)
736 err = got_error_from_errno("close");
737 if (blob)
738 got_object_blob_close(blob);
739 if (matched_id != NULL)
740 free(matched_id);
741 if (err) {
742 if (*fp != NULL)
743 fclose(*fp);
744 if (*path != NULL)
745 unlink(*path);
746 free(*path);
747 *fp = NULL;
748 *path = NULL;
750 return err;
753 static const struct got_error *
754 prepare_merge(int *do_merge, char **apath, FILE **afile,
755 struct got_worktree *worktree, struct got_repository *repo,
756 struct got_patch *p, struct got_object_id *commit_id,
757 struct got_tree_object *tree, const char *path)
759 const struct got_error *err = NULL;
761 *do_merge = 0;
762 *apath = NULL;
763 *afile = NULL;
765 /* don't run the diff3 merge on creations/deletions */
766 if (p->old == NULL || p->new == NULL)
767 return NULL;
769 if (commit_id) {
770 struct got_object_id *id;
772 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
773 if (err)
774 return err;
775 got_sha1_digest_to_str(id->hash, p->blob, sizeof(p->blob));
776 got_sha1_digest_to_str(commit_id->hash, p->cid, sizeof(p->cid));
777 free(id);
778 err = open_blob(apath, afile, p->blob, repo);
779 *do_merge = err == NULL;
780 } else if (*p->blob != '\0') {
781 err = open_blob(apath, afile, p->blob, repo);
782 /*
783 * ignore failures to open this blob, we might have
784 * parsed gibberish.
785 */
786 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
787 err->code != GOT_ERR_NO_OBJ)
788 return err;
789 *do_merge = err == NULL;
790 err = NULL;
793 return err;
796 static const struct got_error *
797 apply_patch(int *overlapcnt, struct got_worktree *worktree,
798 struct got_repository *repo, struct got_fileindex *fileindex,
799 const char *old, const char *new, struct got_patch *p, int nop,
800 int reverse, struct got_object_id *commit_id,
801 struct got_tree_object *tree, struct patch_args *pa,
802 got_cancel_cb cancel_cb, void *cancel_arg)
804 const struct got_error *err = NULL;
805 struct stat sb;
806 int do_merge = 0, file_renamed = 0;
807 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
808 char *oldpath = NULL, *newpath = NULL;
809 char *tmppath = NULL, *template = NULL;
810 char *apath = NULL, *mergepath = NULL;
811 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
812 int outfd;
813 mode_t mode = GOT_DEFAULT_FILE_MODE;
815 *overlapcnt = 0;
817 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
818 commit_id, tree, old);
819 if (err)
820 return err;
822 if (reverse && !do_merge)
823 reverse_patch(p);
825 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
826 old) == -1) {
827 err = got_error_from_errno("asprintf");
828 goto done;
831 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
832 new) == -1) {
833 err = got_error_from_errno("asprintf");
834 goto done;
837 file_renamed = strcmp(oldpath, newpath);
839 if (asprintf(&template, "%s/got-patch",
840 got_worktree_get_root_path(worktree)) == -1) {
841 err = got_error_from_errno(template);
842 goto done;
845 if (p->old != NULL) {
846 if ((oldfile = fopen(oldpath, "r")) == NULL) {
847 err = got_error_from_errno2("open", oldpath);
848 goto done;
850 if (fstat(fileno(oldfile), &sb) == -1) {
851 err = got_error_from_errno2("fstat", oldpath);
852 goto done;
854 mode = sb.st_mode;
855 } else if (p->xbit)
856 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
858 err = got_opentemp_named(&tmppath, &tmpfile, template, "");
859 if (err)
860 goto done;
861 outfd = fileno(tmpfile);
862 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
863 if (err)
864 goto done;
866 if (do_merge) {
867 const char *type, *id;
869 if (fseeko(afile, 0, SEEK_SET) == -1 ||
870 fseeko(oldfile, 0, SEEK_SET) == -1 ||
871 fseeko(tmpfile, 0, SEEK_SET) == -1) {
872 err = got_error_from_errno("fseeko");
873 goto done;
876 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
877 err = got_error_from_errno("asprintf");
878 oldlabel = NULL;
879 goto done;
882 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
883 err = got_error_from_errno("asprintf");
884 newlabel = NULL;
885 goto done;
888 if (*p->cid != '\0') {
889 type = "commit";
890 id = p->cid;
891 } else {
892 type = "blob";
893 id = p->blob;
896 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
897 err = got_error_from_errno("asprintf");
898 anclabel = NULL;
899 goto done;
902 if (reverse) {
903 char *s;
904 FILE *t;
906 s = anclabel;
907 anclabel = newlabel;
908 newlabel = s;
910 t = afile;
911 afile = tmpfile;
912 tmpfile = t;
915 err = got_opentemp_named(&mergepath, &mergefile, template, "");
916 if (err)
917 goto done;
918 outfd = fileno(mergefile);
920 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
921 oldfile, oldlabel, anclabel, newlabel,
922 GOT_DIFF_ALGORITHM_PATIENCE);
923 if (err)
924 goto done;
927 if (nop)
928 goto done;
930 if (p->old != NULL && p->new == NULL) {
931 err = got_worktree_patch_schedule_rm(old, repo, worktree,
932 fileindex, patch_delete, pa);
933 goto done;
936 if (fchmod(outfd, apply_umask(mode)) == -1) {
937 err = got_error_from_errno2("chmod", tmppath);
938 goto done;
941 if (mergepath) {
942 err = got_path_move_file(mergepath, newpath);
943 if (err)
944 goto done;
945 free(mergepath);
946 mergepath = NULL;
947 } else {
948 err = got_path_move_file(tmppath, newpath);
949 if (err)
950 goto done;
951 free(tmppath);
952 tmppath = NULL;
955 if (file_renamed) {
956 err = got_worktree_patch_schedule_rm(old, repo, worktree,
957 fileindex, patch_delete, pa);
958 if (err == NULL)
959 err = got_worktree_patch_schedule_add(new, repo,
960 worktree, fileindex, patch_add,
961 pa);
962 if (err)
963 unlink(newpath);
964 } else if (p->old == NULL) {
965 err = got_worktree_patch_schedule_add(new, repo, worktree,
966 fileindex, patch_add, pa);
967 if (err)
968 unlink(newpath);
969 } else if (*overlapcnt != 0)
970 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
971 else if (do_merge)
972 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
973 else
974 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
976 done:
977 free(template);
979 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
980 err = got_error_from_errno("unlink");
981 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
982 err = got_error_from_errno("fclose");
983 free(tmppath);
985 free(oldpath);
986 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
987 err = got_error_from_errno("fclose");
989 if (apath != NULL && unlink(apath) == -1 && err == NULL)
990 err = got_error_from_errno("unlink");
991 if (afile != NULL && fclose(afile) == EOF && err == NULL)
992 err = got_error_from_errno("fclose");
993 free(apath);
995 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
996 err = got_error_from_errno("unlink");
997 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
998 err = got_error_from_errno("fclose");
999 free(mergepath);
1001 free(newpath);
1002 free(oldlabel);
1003 free(newlabel);
1004 free(anclabel);
1005 return err;
1008 const struct got_error *
1009 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1010 int nop, int strip, int reverse, struct got_object_id *commit_id,
1011 got_patch_progress_cb progress_cb, void *progress_arg,
1012 got_cancel_cb cancel_cb, void *cancel_arg)
1014 const struct got_error *err = NULL, *complete_err = NULL;
1015 struct got_fileindex *fileindex = NULL;
1016 struct got_commit_object *commit = NULL;
1017 struct got_tree_object *tree = NULL;
1018 char *fileindex_path = NULL;
1019 char *oldpath, *newpath;
1020 struct imsgbuf *ibuf;
1021 int imsg_fds[2] = {-1, -1};
1022 int overlapcnt, done = 0, failed = 0;
1023 pid_t pid;
1025 ibuf = calloc(1, sizeof(*ibuf));
1026 if (ibuf == NULL) {
1027 err = got_error_from_errno("calloc");
1028 goto done;
1031 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1032 err = got_error_from_errno("socketpair");
1033 goto done;
1036 pid = fork();
1037 if (pid == -1) {
1038 err = got_error_from_errno("fork");
1039 goto done;
1040 } else if (pid == 0) {
1041 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1042 NULL);
1043 /* not reached */
1046 if (close(imsg_fds[1]) == -1) {
1047 err = got_error_from_errno("close");
1048 goto done;
1050 imsg_fds[1] = -1;
1051 imsg_init(ibuf, imsg_fds[0]);
1053 err = send_patch(ibuf, fd);
1054 fd = -1;
1055 if (err)
1056 goto done;
1058 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1059 worktree);
1060 if (err)
1061 goto done;
1063 if (commit_id) {
1064 err = got_object_open_as_commit(&commit, repo, commit_id);
1065 if (err)
1066 goto done;
1068 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1069 if (err)
1070 goto done;
1073 while (!done && err == NULL) {
1074 struct got_patch p;
1075 struct patch_args pa;
1077 pa.progress_cb = progress_cb;
1078 pa.progress_arg = progress_arg;
1079 pa.head = &p.head;
1081 err = recv_patch(ibuf, &done, &p, strip);
1082 if (err || done)
1083 break;
1085 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1086 &newpath, worktree, repo, fileindex);
1087 if (err == NULL)
1088 err = apply_patch(&overlapcnt, worktree, repo,
1089 fileindex, oldpath, newpath, &p, nop, reverse,
1090 commit_id, tree, &pa, cancel_cb, cancel_arg);
1091 if (err != NULL) {
1092 failed = 1;
1093 /* recoverable errors */
1094 if (err->code == GOT_ERR_FILE_STATUS ||
1095 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1096 err = report_progress(&pa, p.old, p.new,
1097 GOT_STATUS_CANNOT_UPDATE, err);
1098 else if (err->code == GOT_ERR_HUNK_FAILED)
1099 err = report_progress(&pa, p.old, p.new,
1100 GOT_STATUS_CANNOT_UPDATE, NULL);
1102 if (overlapcnt != 0)
1103 failed = 1;
1105 free(oldpath);
1106 free(newpath);
1107 patch_free(&p);
1109 if (err)
1110 break;
1113 done:
1114 if (fileindex != NULL)
1115 complete_err = got_worktree_patch_complete(fileindex,
1116 fileindex_path);
1117 if (complete_err && err == NULL)
1118 err = complete_err;
1119 free(fileindex_path);
1120 if (tree)
1121 got_object_tree_close(tree);
1122 if (commit)
1123 got_object_commit_close(commit);
1124 if (fd != -1 && close(fd) == -1 && err == NULL)
1125 err = got_error_from_errno("close");
1126 if (ibuf != NULL)
1127 imsg_clear(ibuf);
1128 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1129 err = got_error_from_errno("close");
1130 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1131 err = got_error_from_errno("close");
1132 if (err == NULL && failed)
1133 err = got_error(GOT_ERR_PATCH_FAILED);
1134 return err;