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 if (!got_parse_sha1_digest(id.hash, blobid))
710 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
711 idptr = &id;
714 fd = got_opentempfd();
715 if (fd == -1) {
716 err = got_error_from_errno("got_opentempfd");
717 goto done;
720 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
721 if (err)
722 goto done;
724 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
725 "");
726 if (err)
727 goto done;
729 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
730 if (err)
731 goto done;
733 done:
734 if (fd != -1 && close(fd) == -1 && err == NULL)
735 err = got_error_from_errno("close");
736 if (blob)
737 got_object_blob_close(blob);
738 if (matched_id != NULL)
739 free(matched_id);
740 if (err) {
741 if (*fp != NULL)
742 fclose(*fp);
743 if (*path != NULL)
744 unlink(*path);
745 free(*path);
746 *fp = NULL;
747 *path = NULL;
749 return err;
752 static const struct got_error *
753 prepare_merge(int *do_merge, char **apath, FILE **afile,
754 struct got_worktree *worktree, struct got_repository *repo,
755 struct got_patch *p, struct got_object_id *commit_id,
756 struct got_tree_object *tree, const char *path)
758 const struct got_error *err = NULL;
760 *do_merge = 0;
761 *apath = NULL;
762 *afile = NULL;
764 /* don't run the diff3 merge on creations/deletions */
765 if (p->old == NULL || p->new == NULL)
766 return NULL;
768 if (commit_id) {
769 struct got_object_id *id;
771 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
772 if (err)
773 return err;
774 got_sha1_digest_to_str(id->hash, p->blob, sizeof(p->blob));
775 got_sha1_digest_to_str(commit_id->hash, p->cid, sizeof(p->cid));
776 free(id);
777 err = open_blob(apath, afile, p->blob, repo);
778 *do_merge = err == NULL;
779 } else if (*p->blob != '\0') {
780 err = open_blob(apath, afile, p->blob, repo);
781 /*
782 * ignore failures to open this blob, we might have
783 * parsed gibberish.
784 */
785 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
786 err->code != GOT_ERR_NO_OBJ)
787 return err;
788 *do_merge = err == NULL;
789 err = NULL;
792 return err;
795 static const struct got_error *
796 apply_patch(int *overlapcnt, struct got_worktree *worktree,
797 struct got_repository *repo, struct got_fileindex *fileindex,
798 const char *old, const char *new, struct got_patch *p, int nop,
799 int reverse, struct got_object_id *commit_id,
800 struct got_tree_object *tree, struct patch_args *pa,
801 got_cancel_cb cancel_cb, void *cancel_arg)
803 const struct got_error *err = NULL;
804 struct stat sb;
805 int do_merge = 0, file_renamed = 0;
806 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
807 char *oldpath = NULL, *newpath = NULL;
808 char *tmppath = NULL, *template = NULL;
809 char *apath = NULL, *mergepath = NULL;
810 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
811 int outfd;
812 mode_t mode = GOT_DEFAULT_FILE_MODE;
814 *overlapcnt = 0;
816 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
817 commit_id, tree, old);
818 if (err)
819 return err;
821 if (reverse && !do_merge)
822 reverse_patch(p);
824 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
825 old) == -1) {
826 err = got_error_from_errno("asprintf");
827 goto done;
830 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
831 new) == -1) {
832 err = got_error_from_errno("asprintf");
833 goto done;
836 file_renamed = strcmp(oldpath, newpath);
838 if (asprintf(&template, "%s/got-patch",
839 got_worktree_get_root_path(worktree)) == -1) {
840 err = got_error_from_errno(template);
841 goto done;
844 if (p->old != NULL) {
845 if ((oldfile = fopen(oldpath, "r")) == NULL) {
846 err = got_error_from_errno2("open", oldpath);
847 goto done;
849 if (fstat(fileno(oldfile), &sb) == -1) {
850 err = got_error_from_errno2("fstat", oldpath);
851 goto done;
853 mode = sb.st_mode;
854 } else if (p->xbit)
855 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
857 err = got_opentemp_named(&tmppath, &tmpfile, template, "");
858 if (err)
859 goto done;
860 outfd = fileno(tmpfile);
861 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
862 if (err)
863 goto done;
865 if (do_merge) {
866 const char *type, *id;
868 if (fseeko(afile, 0, SEEK_SET) == -1 ||
869 fseeko(oldfile, 0, SEEK_SET) == -1 ||
870 fseeko(tmpfile, 0, SEEK_SET) == -1) {
871 err = got_error_from_errno("fseeko");
872 goto done;
875 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
876 err = got_error_from_errno("asprintf");
877 oldlabel = NULL;
878 goto done;
881 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
882 err = got_error_from_errno("asprintf");
883 newlabel = NULL;
884 goto done;
887 if (*p->cid != '\0') {
888 type = "commit";
889 id = p->cid;
890 } else {
891 type = "blob";
892 id = p->blob;
895 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
896 err = got_error_from_errno("asprintf");
897 anclabel = NULL;
898 goto done;
901 if (reverse) {
902 char *s;
903 FILE *t;
905 s = anclabel;
906 anclabel = newlabel;
907 newlabel = s;
909 t = afile;
910 afile = tmpfile;
911 tmpfile = t;
914 err = got_opentemp_named(&mergepath, &mergefile, template, "");
915 if (err)
916 goto done;
917 outfd = fileno(mergefile);
919 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
920 oldfile, oldlabel, anclabel, newlabel,
921 GOT_DIFF_ALGORITHM_PATIENCE);
922 if (err)
923 goto done;
926 if (nop)
927 goto done;
929 if (p->old != NULL && p->new == NULL) {
930 err = got_worktree_patch_schedule_rm(old, repo, worktree,
931 fileindex, patch_delete, pa);
932 goto done;
935 if (fchmod(outfd, apply_umask(mode)) == -1) {
936 err = got_error_from_errno2("chmod", tmppath);
937 goto done;
940 if (mergepath) {
941 err = got_path_move_file(mergepath, newpath);
942 if (err)
943 goto done;
944 free(mergepath);
945 mergepath = NULL;
946 } else {
947 err = got_path_move_file(tmppath, newpath);
948 if (err)
949 goto done;
950 free(tmppath);
951 tmppath = NULL;
954 if (file_renamed) {
955 err = got_worktree_patch_schedule_rm(old, repo, worktree,
956 fileindex, patch_delete, pa);
957 if (err == NULL)
958 err = got_worktree_patch_schedule_add(new, repo,
959 worktree, fileindex, patch_add,
960 pa);
961 if (err)
962 unlink(newpath);
963 } else if (p->old == NULL) {
964 err = got_worktree_patch_schedule_add(new, repo, worktree,
965 fileindex, patch_add, pa);
966 if (err)
967 unlink(newpath);
968 } else if (*overlapcnt != 0)
969 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
970 else if (do_merge)
971 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
972 else
973 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
975 done:
976 free(template);
978 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
979 err = got_error_from_errno("unlink");
980 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
981 err = got_error_from_errno("fclose");
982 free(tmppath);
984 free(oldpath);
985 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
986 err = got_error_from_errno("fclose");
988 if (apath != NULL && unlink(apath) == -1 && err == NULL)
989 err = got_error_from_errno("unlink");
990 if (afile != NULL && fclose(afile) == EOF && err == NULL)
991 err = got_error_from_errno("fclose");
992 free(apath);
994 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
995 err = got_error_from_errno("unlink");
996 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
997 err = got_error_from_errno("fclose");
998 free(mergepath);
1000 free(newpath);
1001 free(oldlabel);
1002 free(newlabel);
1003 free(anclabel);
1004 return err;
1007 const struct got_error *
1008 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1009 int nop, int strip, int reverse, struct got_object_id *commit_id,
1010 got_patch_progress_cb progress_cb, void *progress_arg,
1011 got_cancel_cb cancel_cb, void *cancel_arg)
1013 const struct got_error *err = NULL, *complete_err = NULL;
1014 struct got_fileindex *fileindex = NULL;
1015 struct got_commit_object *commit = NULL;
1016 struct got_tree_object *tree = NULL;
1017 char *fileindex_path = NULL;
1018 char *oldpath, *newpath;
1019 struct imsgbuf *ibuf;
1020 int imsg_fds[2] = {-1, -1};
1021 int overlapcnt, done = 0, failed = 0;
1022 pid_t pid;
1024 ibuf = calloc(1, sizeof(*ibuf));
1025 if (ibuf == NULL) {
1026 err = got_error_from_errno("calloc");
1027 goto done;
1030 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1031 err = got_error_from_errno("socketpair");
1032 goto done;
1035 pid = fork();
1036 if (pid == -1) {
1037 err = got_error_from_errno("fork");
1038 goto done;
1039 } else if (pid == 0) {
1040 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1041 NULL);
1042 /* not reached */
1045 if (close(imsg_fds[1]) == -1) {
1046 err = got_error_from_errno("close");
1047 goto done;
1049 imsg_fds[1] = -1;
1050 imsg_init(ibuf, imsg_fds[0]);
1052 err = send_patch(ibuf, fd);
1053 fd = -1;
1054 if (err)
1055 goto done;
1057 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1058 worktree);
1059 if (err)
1060 goto done;
1062 if (commit_id) {
1063 err = got_object_open_as_commit(&commit, repo, commit_id);
1064 if (err)
1065 goto done;
1067 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1068 if (err)
1069 goto done;
1072 while (!done && err == NULL) {
1073 struct got_patch p;
1074 struct patch_args pa;
1076 pa.progress_cb = progress_cb;
1077 pa.progress_arg = progress_arg;
1078 pa.head = &p.head;
1080 err = recv_patch(ibuf, &done, &p, strip);
1081 if (err || done)
1082 break;
1084 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1085 &newpath, worktree, repo, fileindex);
1086 if (err == NULL)
1087 err = apply_patch(&overlapcnt, worktree, repo,
1088 fileindex, oldpath, newpath, &p, nop, reverse,
1089 commit_id, tree, &pa, cancel_cb, cancel_arg);
1090 if (err != NULL) {
1091 failed = 1;
1092 /* recoverable errors */
1093 if (err->code == GOT_ERR_FILE_STATUS ||
1094 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1095 err = report_progress(&pa, p.old, p.new,
1096 GOT_STATUS_CANNOT_UPDATE, err);
1097 else if (err->code == GOT_ERR_HUNK_FAILED)
1098 err = report_progress(&pa, p.old, p.new,
1099 GOT_STATUS_CANNOT_UPDATE, NULL);
1101 if (overlapcnt != 0)
1102 failed = 1;
1104 free(oldpath);
1105 free(newpath);
1106 patch_free(&p);
1108 if (err)
1109 break;
1112 done:
1113 if (fileindex != NULL)
1114 complete_err = got_worktree_patch_complete(fileindex,
1115 fileindex_path);
1116 if (complete_err && err == NULL)
1117 err = complete_err;
1118 free(fileindex_path);
1119 if (tree)
1120 got_object_tree_close(tree);
1121 if (commit)
1122 got_object_commit_close(commit);
1123 if (fd != -1 && close(fd) == -1 && err == NULL)
1124 err = got_error_from_errno("close");
1125 if (ibuf != NULL)
1126 imsg_clear(ibuf);
1127 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1128 err = got_error_from_errno("close");
1129 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1130 err = got_error_from_errno("close");
1131 if (err == NULL && failed)
1132 err = got_error(GOT_ERR_PATCH_FAILED);
1133 return err;