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 char *old;
78 char *new;
79 char cid[41];
80 char blob[41];
81 struct got_patch_hunk_head head;
82 };
84 struct patch_args {
85 got_patch_progress_cb progress_cb;
86 void *progress_arg;
87 struct got_patch_hunk_head *head;
88 };
90 static const struct got_error *
91 send_patch(struct imsgbuf *ibuf, int fd)
92 {
93 const struct got_error *err = NULL;
95 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
96 NULL, 0) == -1) {
97 err = got_error_from_errno(
98 "imsg_compose GOT_IMSG_PATCH_FILE");
99 close(fd);
100 return err;
103 return got_privsep_flush_imsg(ibuf);
106 static void
107 patch_free(struct got_patch *p)
109 struct got_patch_hunk *h;
110 size_t i;
112 while (!STAILQ_EMPTY(&p->head)) {
113 h = STAILQ_FIRST(&p->head);
114 STAILQ_REMOVE_HEAD(&p->head, entries);
116 for (i = 0; i < h->len; ++i)
117 free(h->lines[i]);
118 free(h->lines);
119 free(h);
122 free(p->new);
123 free(p->old);
125 memset(p, 0, sizeof(*p));
126 STAILQ_INIT(&p->head);
129 static const struct got_error *
130 pushline(struct got_patch_hunk *h, const char *line)
132 void *t;
133 size_t newcap;
135 if (h->len == h->cap) {
136 if ((newcap = h->cap * 1.5) == 0)
137 newcap = 16;
138 t = recallocarray(h->lines, h->cap, newcap,
139 sizeof(h->lines[0]));
140 if (t == NULL)
141 return got_error_from_errno("recallocarray");
142 h->lines = t;
143 h->cap = newcap;
146 if ((t = strdup(line)) == NULL)
147 return got_error_from_errno("strdup");
149 h->lines[h->len++] = t;
150 return NULL;
153 static const struct got_error *
154 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
156 const struct got_error *err = NULL;
157 struct imsg imsg;
158 struct got_imsg_patch_hunk hdr;
159 struct got_imsg_patch patch;
160 struct got_patch_hunk *h = NULL;
161 size_t datalen;
162 int lastmode = -1;
164 memset(p, 0, sizeof(*p));
165 STAILQ_INIT(&p->head);
167 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
168 if (err)
169 return err;
170 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
171 *done = 1;
172 goto done;
174 if (imsg.hdr.type != GOT_IMSG_PATCH) {
175 err = got_error(GOT_ERR_PRIVSEP_MSG);
176 goto done;
178 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
179 if (datalen != sizeof(patch)) {
180 err = got_error(GOT_ERR_PRIVSEP_LEN);
181 goto done;
183 memcpy(&patch, imsg.data, sizeof(patch));
185 if (patch.old[sizeof(patch.old)-1] != '\0' ||
186 patch.new[sizeof(patch.new)-1] != '\0' ||
187 patch.cid[sizeof(patch.cid)-1] != '\0' ||
188 patch.blob[sizeof(patch.blob)-1] != '\0') {
189 err = got_error(GOT_ERR_PRIVSEP_LEN);
190 goto done;
193 if (*patch.cid != '\0')
194 strlcpy(p->cid, patch.cid, sizeof(p->cid));
196 if (*patch.blob != '\0')
197 strlcpy(p->blob, patch.blob, sizeof(p->blob));
199 /* automatically set strip=1 for git-style diffs */
200 if (strip == -1 && patch.git &&
201 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
202 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
203 strip = 1;
205 /* prefer the new name if not /dev/null for not git-style diffs */
206 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
207 err = got_path_strip(&p->old, patch.new, strip);
208 if (err)
209 goto done;
210 } else if (*patch.old != '\0') {
211 err = got_path_strip(&p->old, patch.old, strip);
212 if (err)
213 goto done;
216 if (*patch.new != '\0') {
217 err = got_path_strip(&p->new, patch.new, strip);
218 if (err)
219 goto done;
222 if (p->old == NULL && p->new == NULL) {
223 err = got_error(GOT_ERR_PATCH_MALFORMED);
224 goto done;
227 imsg_free(&imsg);
229 for (;;) {
230 char *t;
232 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
233 if (err) {
234 patch_free(p);
235 return err;
238 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
239 switch (imsg.hdr.type) {
240 case GOT_IMSG_PATCH_DONE:
241 if (h != NULL && h->len == 0)
242 err = got_error(GOT_ERR_PATCH_MALFORMED);
243 goto done;
244 case GOT_IMSG_PATCH_HUNK:
245 if (h != NULL &&
246 (h->len == 0 || h->old_nonl || h->new_nonl)) {
247 err = got_error(GOT_ERR_PATCH_MALFORMED);
248 goto done;
250 lastmode = -1;
251 if (datalen != sizeof(hdr)) {
252 err = got_error(GOT_ERR_PRIVSEP_LEN);
253 goto done;
255 memcpy(&hdr, imsg.data, sizeof(hdr));
256 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
257 err = got_error(GOT_ERR_PRIVSEP_LEN);
258 goto done;
260 if ((h = calloc(1, sizeof(*h))) == NULL) {
261 err = got_error_from_errno("calloc");
262 goto done;
264 h->old_from = hdr.oldfrom;
265 h->old_lines = hdr.oldlines;
266 h->new_from = hdr.newfrom;
267 h->new_lines = hdr.newlines;
268 STAILQ_INSERT_TAIL(&p->head, h, entries);
269 break;
270 case GOT_IMSG_PATCH_LINE:
271 if (h == NULL) {
272 err = got_error(GOT_ERR_PRIVSEP_MSG);
273 goto done;
275 t = imsg.data;
276 /* at least one char */
277 if (datalen < 2 || t[datalen-1] != '\0') {
278 err = got_error(GOT_ERR_PRIVSEP_MSG);
279 goto done;
281 if (*t != ' ' && *t != '-' && *t != '+' &&
282 *t != '\\') {
283 err = got_error(GOT_ERR_PRIVSEP_MSG);
284 goto done;
287 if (*t != '\\')
288 err = pushline(h, t);
289 else if (lastmode == '-')
290 h->old_nonl = 1;
291 else if (lastmode == '+')
292 h->new_nonl = 1;
293 else
294 err = got_error(GOT_ERR_PATCH_MALFORMED);
296 if (err)
297 goto done;
299 lastmode = *t;
300 break;
301 default:
302 err = got_error(GOT_ERR_PRIVSEP_MSG);
303 goto done;
306 imsg_free(&imsg);
309 done:
310 if (err)
311 patch_free(p);
313 imsg_free(&imsg);
314 return err;
317 static void
318 reverse_patch(struct got_patch *p)
320 struct got_patch_hunk *h;
321 size_t i;
322 int tmp;
324 STAILQ_FOREACH(h, &p->head, entries) {
325 tmp = h->old_from;
326 h->old_from = h->new_from;
327 h->new_from = tmp;
329 tmp = h->old_lines;
330 h->old_lines = h->new_lines;
331 h->new_lines = tmp;
333 tmp = h->old_nonl;
334 h->old_nonl = h->new_nonl;
335 h->new_nonl = tmp;
337 for (i = 0; i < h->len; ++i) {
338 if (*h->lines[i] == '+')
339 *h->lines[i] = '-';
340 else if (*h->lines[i] == '-')
341 *h->lines[i] = '+';
346 /*
347 * Copy data from orig starting at copypos until pos into tmp.
348 * If pos is -1, copy until EOF.
349 */
350 static const struct got_error *
351 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
353 char buf[BUFSIZ];
354 size_t len, r, w;
356 if (fseeko(orig, copypos, SEEK_SET) == -1)
357 return got_error_from_errno("fseeko");
359 while (pos == -1 || copypos < pos) {
360 len = sizeof(buf);
361 if (pos > 0)
362 len = MIN(len, (size_t)pos - copypos);
363 r = fread(buf, 1, len, orig);
364 if (r != len && ferror(orig))
365 return got_error_from_errno("fread");
366 w = fwrite(buf, 1, r, tmp);
367 if (w != r)
368 return got_error_from_errno("fwrite");
369 copypos += len;
370 if (r != len && feof(orig)) {
371 if (pos == -1)
372 return NULL;
373 return got_error(GOT_ERR_HUNK_FAILED);
376 return NULL;
379 static int linecmp(const char *, const char *, int *);
381 static const struct got_error *
382 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
384 const struct got_error *err = NULL;
385 char *line = NULL;
386 char mode = *h->lines[0];
387 size_t linesize = 0;
388 ssize_t linelen;
389 off_t match = -1;
390 int mangled = 0, match_lineno = -1;
392 for (;;) {
393 linelen = getline(&line, &linesize, orig);
394 if (linelen == -1) {
395 if (ferror(orig))
396 err = got_error_from_errno("getline");
397 else if (match == -1)
398 err = got_error(GOT_ERR_HUNK_FAILED);
399 break;
401 if (line[linelen - 1] == '\n')
402 line[linelen - 1] = '\0';
403 (*lineno)++;
405 if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
406 (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
407 (mode == '+' && *lineno == h->old_from)) {
408 match = ftello(orig);
409 if (match == -1) {
410 err = got_error_from_errno("ftello");
411 break;
413 match -= linelen;
414 match_lineno = (*lineno)-1;
417 if (*lineno >= h->old_from && match != -1) {
418 if (mangled)
419 h->ws_mangled = 1;
420 break;
424 if (err == NULL) {
425 *pos = match;
426 *lineno = match_lineno;
427 if (fseeko(orig, match, SEEK_SET) == -1)
428 err = got_error_from_errno("fseeko");
431 free(line);
432 return err;
435 static int
436 linecmp(const char *a, const char *b, int *mangled)
438 int c;
440 *mangled = 0;
441 c = strcmp(a, b);
442 if (c == 0)
443 return c;
445 *mangled = 1;
446 for (;;) {
447 while (*a == '\t' || *a == ' ' || *a == '\f')
448 a++;
449 while (*b == '\t' || *b == ' ' || *b == '\f')
450 b++;
451 if (*a == '\0' || *a != *b)
452 break;
453 a++, b++;
456 return *a - *b;
459 static const struct got_error *
460 test_hunk(FILE *orig, struct got_patch_hunk *h)
462 const struct got_error *err = NULL;
463 char *line = NULL;
464 size_t linesize = 0, i = 0;
465 ssize_t linelen;
466 int mangled;
468 for (i = 0; i < h->len; ++i) {
469 switch (*h->lines[i]) {
470 case '+':
471 continue;
472 case ' ':
473 case '-':
474 linelen = getline(&line, &linesize, orig);
475 if (linelen == -1) {
476 if (ferror(orig))
477 err = got_error_from_errno("getline");
478 else
479 err = got_error(
480 GOT_ERR_HUNK_FAILED);
481 goto done;
483 if (line[linelen - 1] == '\n')
484 line[linelen - 1] = '\0';
485 if (linecmp(h->lines[i] + 1, line, &mangled)) {
486 err = got_error(GOT_ERR_HUNK_FAILED);
487 goto done;
489 if (mangled)
490 h->ws_mangled = 1;
491 break;
495 done:
496 free(line);
497 return err;
500 static const struct got_error *
501 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
502 off_t from)
504 const struct got_error *err = NULL;
505 const char *t;
506 size_t linesize = 0, i, new = 0;
507 char *line = NULL;
508 char mode;
509 ssize_t linelen;
511 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
512 return got_error_from_errno("fseeko");
514 for (i = 0; i < h->len; ++i) {
515 switch (mode = *h->lines[i]) {
516 case '-':
517 case ' ':
518 (*lineno)++;
519 if (orig != NULL) {
520 linelen = getline(&line, &linesize, orig);
521 if (linelen == -1) {
522 err = got_error_from_errno("getline");
523 goto done;
525 if (line[linelen - 1] == '\n')
526 line[linelen - 1] = '\0';
527 t = line;
528 } else
529 t = h->lines[i] + 1;
530 if (mode == '-')
531 continue;
532 if (fprintf(tmp, "%s\n", t) < 0) {
533 err = got_error_from_errno("fprintf");
534 goto done;
536 break;
537 case '+':
538 new++;
539 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
540 err = got_error_from_errno("fprintf");
541 goto done;
543 if (new != h->new_lines || !h->new_nonl) {
544 if (fprintf(tmp, "\n") < 0) {
545 err = got_error_from_errno("fprintf");
546 goto done;
549 break;
553 done:
554 free(line);
555 return err;
558 static const struct got_error *
559 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
561 const struct got_error *err = NULL;
562 struct got_patch_hunk *h;
563 struct stat sb;
564 int lineno = 0;
565 off_t copypos, pos;
566 char *line = NULL;
567 size_t linesize = 0;
568 ssize_t linelen;
570 if (p->old == NULL) { /* create */
571 h = STAILQ_FIRST(&p->head);
572 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
573 return got_error(GOT_ERR_PATCH_MALFORMED);
574 return apply_hunk(orig, tmp, h, &lineno, 0);
577 if (fstat(fileno(orig), &sb) == -1)
578 return got_error_from_errno("fstat");
580 copypos = 0;
581 STAILQ_FOREACH(h, &p->head, entries) {
582 tryagain:
583 err = locate_hunk(orig, h, &pos, &lineno);
584 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
585 h->err = err;
586 if (err != NULL)
587 return err;
588 err = copy(tmp, orig, copypos, pos);
589 if (err != NULL)
590 return err;
591 copypos = pos;
593 err = test_hunk(orig, h);
594 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
595 /*
596 * try to apply the hunk again starting the search
597 * after the previous partial match.
598 */
599 if (fseeko(orig, pos, SEEK_SET) == -1)
600 return got_error_from_errno("fseeko");
601 linelen = getline(&line, &linesize, orig);
602 if (linelen == -1)
603 return got_error_from_errno("getline");
604 lineno++;
605 goto tryagain;
607 if (err != NULL)
608 return err;
610 if (lineno + 1 != h->old_from)
611 h->offset = lineno + 1 - h->old_from;
613 err = apply_hunk(orig, tmp, h, &lineno, pos);
614 if (err != NULL)
615 return err;
617 copypos = ftello(orig);
618 if (copypos == -1)
619 return got_error_from_errno("ftello");
622 if (p->new == NULL && sb.st_size != copypos) {
623 h = STAILQ_FIRST(&p->head);
624 h->err = got_error(GOT_ERR_HUNK_FAILED);
625 err = h->err;
626 } else if (!feof(orig))
627 err = copy(tmp, orig, copypos, -1);
629 return err;
632 static const struct got_error *
633 report_progress(struct patch_args *pa, const char *old, const char *new,
634 unsigned char status, const struct got_error *orig_error)
636 const struct got_error *err;
637 struct got_patch_hunk *h;
639 err = pa->progress_cb(pa->progress_arg, old, new, status,
640 orig_error, 0, 0, 0, 0, 0, 0, NULL);
641 if (err)
642 return err;
644 STAILQ_FOREACH(h, pa->head, entries) {
645 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
646 continue;
648 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
649 h->old_from, h->old_lines, h->new_from, h->new_lines,
650 h->offset, h->ws_mangled, h->err);
651 if (err)
652 return err;
655 return NULL;
658 static const struct got_error *
659 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
660 const char *path)
662 return report_progress(arg, path, NULL, status, NULL);
665 static const struct got_error *
666 patch_add(void *arg, unsigned char status, const char *path)
668 return report_progress(arg, NULL, path, status, NULL);
671 static const struct got_error *
672 open_blob(char **path, FILE **fp, const char *blobid,
673 struct got_repository *repo)
675 const struct got_error *err = NULL;
676 struct got_blob_object *blob = NULL;
677 struct got_object_id id, *idptr, *matched_id = NULL;
678 int fd = -1;
680 *fp = NULL;
681 *path = NULL;
683 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
684 err = got_repo_match_object_id(&matched_id, NULL, blobid,
685 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
686 repo);
687 if (err)
688 return err;
689 idptr = matched_id;
690 } else {
691 if (!got_parse_sha1_digest(id.sha1, blobid))
692 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
693 idptr = &id;
696 fd = got_opentempfd();
697 if (fd == -1) {
698 err = got_error_from_errno("got_opentempfd");
699 goto done;
702 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
703 if (err)
704 goto done;
706 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
707 if (err)
708 goto done;
710 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
711 if (err)
712 goto done;
714 done:
715 if (fd != -1 && close(fd) == -1 && err == NULL)
716 err = got_error_from_errno("close");
717 if (blob)
718 got_object_blob_close(blob);
719 if (matched_id != NULL)
720 free(matched_id);
721 if (err) {
722 if (*fp != NULL)
723 fclose(*fp);
724 if (*path != NULL)
725 unlink(*path);
726 free(*path);
727 *fp = NULL;
728 *path = NULL;
730 return err;
733 static const struct got_error *
734 prepare_merge(int *do_merge, char **apath, FILE **afile,
735 struct got_worktree *worktree, struct got_repository *repo,
736 struct got_patch *p, struct got_object_id *commit_id,
737 struct got_tree_object *tree, const char *path)
739 const struct got_error *err = NULL;
741 *do_merge = 0;
742 *apath = NULL;
743 *afile = NULL;
745 /* don't run the diff3 merge on creations/deletions */
746 if (p->old == NULL || p->new == NULL)
747 return NULL;
749 if (commit_id) {
750 struct got_object_id *id;
752 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
753 if (err)
754 return err;
755 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
756 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
757 free(id);
758 err = open_blob(apath, afile, p->blob, repo);
759 *do_merge = err == NULL;
760 } else if (*p->blob != '\0') {
761 err = open_blob(apath, afile, p->blob, repo);
762 /*
763 * ignore failures to open this blob, we might have
764 * parsed gibberish.
765 */
766 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
767 err->code != GOT_ERR_NO_OBJ)
768 return err;
769 *do_merge = err == NULL;
770 err = NULL;
773 return err;
776 static const struct got_error *
777 apply_patch(int *overlapcnt, struct got_worktree *worktree,
778 struct got_repository *repo, struct got_fileindex *fileindex,
779 const char *old, const char *new, struct got_patch *p, int nop,
780 int reverse, struct got_object_id *commit_id,
781 struct got_tree_object *tree, struct patch_args *pa,
782 got_cancel_cb cancel_cb, void *cancel_arg)
784 const struct got_error *err = NULL;
785 struct stat sb;
786 int do_merge = 0, file_renamed = 0;
787 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
788 char *oldpath = NULL, *newpath = NULL;
789 char *tmppath = NULL, *template = NULL, *parent = NULL;
790 char *apath = NULL, *mergepath = NULL;
791 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
792 int outfd;
793 const char *outpath;
794 mode_t mode = GOT_DEFAULT_FILE_MODE;
796 *overlapcnt = 0;
798 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
799 commit_id, tree, old);
800 if (err)
801 return err;
803 if (reverse && !do_merge)
804 reverse_patch(p);
806 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
807 old) == -1) {
808 err = got_error_from_errno("asprintf");
809 goto done;
812 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
813 new) == -1) {
814 err = got_error_from_errno("asprintf");
815 goto done;
818 file_renamed = strcmp(oldpath, newpath);
820 if (asprintf(&template, "%s/got-patch",
821 got_worktree_get_root_path(worktree)) == -1) {
822 err = got_error_from_errno(template);
823 goto done;
826 if (p->old != NULL) {
827 if ((oldfile = fopen(oldpath, "r")) == NULL) {
828 err = got_error_from_errno2("open", oldpath);
829 goto done;
831 if (fstat(fileno(oldfile), &sb) == -1) {
832 err = got_error_from_errno2("fstat", oldpath);
833 goto done;
835 mode = sb.st_mode;
838 err = got_opentemp_named(&tmppath, &tmpfile, template);
839 if (err)
840 goto done;
841 outpath = tmppath;
842 outfd = fileno(tmpfile);
843 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
844 if (err)
845 goto done;
847 if (do_merge) {
848 const char *type, *id;
850 if (fseeko(afile, 0, SEEK_SET) == -1 ||
851 fseeko(oldfile, 0, SEEK_SET) == -1 ||
852 fseeko(tmpfile, 0, SEEK_SET) == -1) {
853 err = got_error_from_errno("fseeko");
854 goto done;
857 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
858 err = got_error_from_errno("asprintf");
859 oldlabel = NULL;
860 goto done;
863 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
864 err = got_error_from_errno("asprintf");
865 newlabel = NULL;
866 goto done;
869 if (*p->cid != '\0') {
870 type = "commit";
871 id = p->cid;
872 } else {
873 type = "blob";
874 id = p->blob;
877 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
878 err = got_error_from_errno("asprintf");
879 anclabel = NULL;
880 goto done;
883 if (reverse) {
884 char *s;
885 FILE *t;
887 s = anclabel;
888 anclabel = newlabel;
889 newlabel = s;
891 t = afile;
892 afile = tmpfile;
893 tmpfile = t;
896 err = got_opentemp_named(&mergepath, &mergefile, template);
897 if (err)
898 goto done;
899 outpath = mergepath;
900 outfd = fileno(mergefile);
902 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
903 oldfile, oldlabel, anclabel, newlabel,
904 GOT_DIFF_ALGORITHM_PATIENCE);
905 if (err)
906 goto done;
909 if (nop)
910 goto done;
912 if (p->old != NULL && p->new == NULL) {
913 err = got_worktree_patch_schedule_rm(old, repo, worktree,
914 fileindex, patch_delete, pa);
915 goto done;
918 if (fchmod(outfd, mode) == -1) {
919 err = got_error_from_errno2("chmod", tmppath);
920 goto done;
923 if (rename(outpath, newpath) == -1) {
924 if (errno != ENOENT) {
925 err = got_error_from_errno3("rename", outpath,
926 newpath);
927 goto done;
930 err = got_path_dirname(&parent, newpath);
931 if (err != NULL)
932 goto done;
933 err = got_path_mkdir(parent);
934 if (err != NULL)
935 goto done;
936 if (rename(outpath, newpath) == -1) {
937 err = got_error_from_errno3("rename", outpath,
938 newpath);
939 goto done;
943 if (file_renamed) {
944 err = got_worktree_patch_schedule_rm(old, repo, worktree,
945 fileindex, patch_delete, pa);
946 if (err == NULL)
947 err = got_worktree_patch_schedule_add(new, repo,
948 worktree, fileindex, patch_add,
949 pa);
950 if (err)
951 unlink(newpath);
952 } else if (p->old == NULL) {
953 err = got_worktree_patch_schedule_add(new, repo, worktree,
954 fileindex, patch_add, pa);
955 if (err)
956 unlink(newpath);
957 } else if (*overlapcnt != 0)
958 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
959 else if (do_merge)
960 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
961 else
962 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
964 done:
965 free(parent);
966 free(template);
968 if (tmppath != NULL)
969 unlink(tmppath);
970 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
971 err = got_error_from_errno("fclose");
972 free(tmppath);
974 free(oldpath);
975 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
976 err = got_error_from_errno("fclose");
978 if (apath != NULL)
979 unlink(apath);
980 if (afile != NULL && fclose(afile) == EOF && err == NULL)
981 err = got_error_from_errno("fclose");
982 free(apath);
984 if (mergepath != NULL)
985 unlink(mergepath);
986 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
987 err = got_error_from_errno("fclose");
988 free(mergepath);
990 free(newpath);
991 free(oldlabel);
992 free(newlabel);
993 free(anclabel);
994 return err;
997 const struct got_error *
998 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
999 int nop, int strip, int reverse, struct got_object_id *commit_id,
1000 got_patch_progress_cb progress_cb, void *progress_arg,
1001 got_cancel_cb cancel_cb, void *cancel_arg)
1003 const struct got_error *err = NULL, *complete_err = NULL;
1004 struct got_fileindex *fileindex = NULL;
1005 struct got_commit_object *commit = NULL;
1006 struct got_tree_object *tree = NULL;
1007 char *fileindex_path = NULL;
1008 char *oldpath, *newpath;
1009 struct imsgbuf *ibuf;
1010 int imsg_fds[2] = {-1, -1};
1011 int overlapcnt, done = 0, failed = 0;
1012 pid_t pid;
1014 ibuf = calloc(1, sizeof(*ibuf));
1015 if (ibuf == NULL) {
1016 err = got_error_from_errno("calloc");
1017 goto done;
1020 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1021 err = got_error_from_errno("socketpair");
1022 goto done;
1025 pid = fork();
1026 if (pid == -1) {
1027 err = got_error_from_errno("fork");
1028 goto done;
1029 } else if (pid == 0) {
1030 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1031 NULL);
1032 /* not reached */
1035 if (close(imsg_fds[1]) == -1) {
1036 err = got_error_from_errno("close");
1037 goto done;
1039 imsg_fds[1] = -1;
1040 imsg_init(ibuf, imsg_fds[0]);
1042 err = send_patch(ibuf, fd);
1043 fd = -1;
1044 if (err)
1045 goto done;
1047 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1048 worktree);
1049 if (err)
1050 goto done;
1052 if (commit_id) {
1053 err = got_object_open_as_commit(&commit, repo, commit_id);
1054 if (err)
1055 goto done;
1057 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1058 if (err)
1059 goto done;
1062 while (!done && err == NULL) {
1063 struct got_patch p;
1064 struct patch_args pa;
1066 pa.progress_cb = progress_cb;
1067 pa.progress_arg = progress_arg;
1068 pa.head = &p.head;
1070 err = recv_patch(ibuf, &done, &p, strip);
1071 if (err || done)
1072 break;
1074 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1075 &newpath, worktree, repo, fileindex);
1076 if (err == NULL)
1077 err = apply_patch(&overlapcnt, worktree, repo,
1078 fileindex, oldpath, newpath, &p, nop, reverse,
1079 commit_id, tree, &pa, cancel_cb, cancel_arg);
1080 if (err != NULL) {
1081 failed = 1;
1082 /* recoverable errors */
1083 if (err->code == GOT_ERR_FILE_STATUS ||
1084 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1085 err = report_progress(&pa, p.old, p.new,
1086 GOT_STATUS_CANNOT_UPDATE, err);
1087 else if (err->code == GOT_ERR_HUNK_FAILED)
1088 err = report_progress(&pa, p.old, p.new,
1089 GOT_STATUS_CANNOT_UPDATE, NULL);
1091 if (overlapcnt != 0)
1092 failed = 1;
1094 free(oldpath);
1095 free(newpath);
1096 patch_free(&p);
1098 if (err)
1099 break;
1102 done:
1103 if (fileindex != NULL)
1104 complete_err = got_worktree_patch_complete(fileindex,
1105 fileindex_path);
1106 if (complete_err && err == NULL)
1107 err = complete_err;
1108 free(fileindex_path);
1109 if (tree)
1110 got_object_tree_close(tree);
1111 if (commit)
1112 got_object_commit_close(commit);
1113 if (fd != -1 && close(fd) == -1 && err == NULL)
1114 err = got_error_from_errno("close");
1115 if (ibuf != NULL)
1116 imsg_clear(ibuf);
1117 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1118 err = got_error_from_errno("close");
1119 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1120 err = got_error_from_errno("close");
1121 if (err == NULL && failed)
1122 err = got_error(GOT_ERR_PATCH_FAILED);
1123 return err;