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 <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_reference.h"
42 #include "got_cancel.h"
43 #include "got_worktree.h"
44 #include "got_repository.h"
45 #include "got_opentemp.h"
46 #include "got_patch.h"
47 #include "got_diff.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_diff.h"
51 #include "got_lib_object.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_sha1.h"
55 #define MIN(a, b) ((a) < (b) ? (a) : (b))
57 struct got_patch_hunk {
58 STAILQ_ENTRY(got_patch_hunk) entries;
59 const struct got_error *err;
60 int ws_mangled;
61 int offset;
62 int old_nonl;
63 int new_nonl;
64 int old_from;
65 int old_lines;
66 int new_from;
67 int new_lines;
68 size_t len;
69 size_t cap;
70 char **lines;
71 };
73 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
74 struct got_patch {
75 char *old;
76 char *new;
77 char cid[41];
78 char blob[41];
79 struct got_patch_hunk_head head;
80 };
82 struct patch_args {
83 got_patch_progress_cb progress_cb;
84 void *progress_arg;
85 struct got_patch_hunk_head *head;
86 };
88 static const struct got_error *
89 send_patch(struct imsgbuf *ibuf, int fd)
90 {
91 const struct got_error *err = NULL;
93 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
94 NULL, 0) == -1) {
95 err = got_error_from_errno(
96 "imsg_compose GOT_IMSG_PATCH_FILE");
97 close(fd);
98 return err;
99 }
101 return got_privsep_flush_imsg(ibuf);
104 static void
105 patch_free(struct got_patch *p)
107 struct got_patch_hunk *h;
108 size_t i;
110 while (!STAILQ_EMPTY(&p->head)) {
111 h = STAILQ_FIRST(&p->head);
112 STAILQ_REMOVE_HEAD(&p->head, entries);
114 for (i = 0; i < h->len; ++i)
115 free(h->lines[i]);
116 free(h->lines);
117 free(h);
120 free(p->new);
121 free(p->old);
123 memset(p, 0, sizeof(*p));
124 STAILQ_INIT(&p->head);
127 static const struct got_error *
128 pushline(struct got_patch_hunk *h, const char *line)
130 void *t;
131 size_t newcap;
133 if (h->len == h->cap) {
134 if ((newcap = h->cap * 1.5) == 0)
135 newcap = 16;
136 t = recallocarray(h->lines, h->cap, newcap,
137 sizeof(h->lines[0]));
138 if (t == NULL)
139 return got_error_from_errno("recallocarray");
140 h->lines = t;
141 h->cap = newcap;
144 if ((t = strdup(line)) == NULL)
145 return got_error_from_errno("strdup");
147 h->lines[h->len++] = t;
148 return NULL;
151 static const struct got_error *
152 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
154 const struct got_error *err = NULL;
155 struct imsg imsg;
156 struct got_imsg_patch_hunk hdr;
157 struct got_imsg_patch patch;
158 struct got_patch_hunk *h = NULL;
159 size_t datalen;
160 int lastmode = -1;
162 memset(p, 0, sizeof(*p));
163 STAILQ_INIT(&p->head);
165 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
166 if (err)
167 return err;
168 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
169 *done = 1;
170 goto done;
172 if (imsg.hdr.type != GOT_IMSG_PATCH) {
173 err = got_error(GOT_ERR_PRIVSEP_MSG);
174 goto done;
176 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
177 if (datalen != sizeof(patch)) {
178 err = got_error(GOT_ERR_PRIVSEP_LEN);
179 goto done;
181 memcpy(&patch, imsg.data, sizeof(patch));
183 if (patch.old[sizeof(patch.old)-1] != '\0' ||
184 patch.new[sizeof(patch.new)-1] != '\0' ||
185 patch.cid[sizeof(patch.cid)-1] != '\0' ||
186 patch.blob[sizeof(patch.blob)-1] != '\0') {
187 err = got_error(GOT_ERR_PRIVSEP_LEN);
188 goto done;
191 if (*patch.cid != '\0')
192 strlcpy(p->cid, patch.cid, sizeof(p->cid));
194 if (*patch.blob != '\0')
195 strlcpy(p->blob, patch.blob, sizeof(p->blob));
197 /* automatically set strip=1 for git-style diffs */
198 if (strip == -1 && patch.git &&
199 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
200 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
201 strip = 1;
203 /* prefer the new name if not /dev/null for not git-style diffs */
204 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
205 err = got_path_strip(&p->old, patch.new, strip);
206 if (err)
207 goto done;
208 } else if (*patch.old != '\0') {
209 err = got_path_strip(&p->old, patch.old, strip);
210 if (err)
211 goto done;
214 if (*patch.new != '\0') {
215 err = got_path_strip(&p->new, patch.new, strip);
216 if (err)
217 goto done;
220 if (p->old == NULL && p->new == NULL) {
221 err = got_error(GOT_ERR_PATCH_MALFORMED);
222 goto done;
225 imsg_free(&imsg);
227 for (;;) {
228 char *t;
230 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
231 if (err) {
232 patch_free(p);
233 return err;
236 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
237 switch (imsg.hdr.type) {
238 case GOT_IMSG_PATCH_DONE:
239 if (h != NULL && h->len == 0)
240 err = got_error(GOT_ERR_PATCH_MALFORMED);
241 goto done;
242 case GOT_IMSG_PATCH_HUNK:
243 if (h != NULL &&
244 (h->len == 0 || h->old_nonl || h->new_nonl)) {
245 err = got_error(GOT_ERR_PATCH_MALFORMED);
246 goto done;
248 lastmode = -1;
249 if (datalen != sizeof(hdr)) {
250 err = got_error(GOT_ERR_PRIVSEP_LEN);
251 goto done;
253 memcpy(&hdr, imsg.data, sizeof(hdr));
254 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
255 err = got_error(GOT_ERR_PRIVSEP_LEN);
256 goto done;
258 if ((h = calloc(1, sizeof(*h))) == NULL) {
259 err = got_error_from_errno("calloc");
260 goto done;
262 h->old_from = hdr.oldfrom;
263 h->old_lines = hdr.oldlines;
264 h->new_from = hdr.newfrom;
265 h->new_lines = hdr.newlines;
266 STAILQ_INSERT_TAIL(&p->head, h, entries);
267 break;
268 case GOT_IMSG_PATCH_LINE:
269 if (h == NULL) {
270 err = got_error(GOT_ERR_PRIVSEP_MSG);
271 goto done;
273 t = imsg.data;
274 /* at least one char */
275 if (datalen < 2 || t[datalen-1] != '\0') {
276 err = got_error(GOT_ERR_PRIVSEP_MSG);
277 goto done;
279 if (*t != ' ' && *t != '-' && *t != '+' &&
280 *t != '\\') {
281 err = got_error(GOT_ERR_PRIVSEP_MSG);
282 goto done;
285 if (*t != '\\')
286 err = pushline(h, t);
287 else if (lastmode == '-')
288 h->old_nonl = 1;
289 else if (lastmode == '+')
290 h->new_nonl = 1;
291 else
292 err = got_error(GOT_ERR_PATCH_MALFORMED);
294 if (err)
295 goto done;
297 lastmode = *t;
298 break;
299 default:
300 err = got_error(GOT_ERR_PRIVSEP_MSG);
301 goto done;
304 imsg_free(&imsg);
307 done:
308 if (err)
309 patch_free(p);
311 imsg_free(&imsg);
312 return err;
315 static void
316 reverse_patch(struct got_patch *p)
318 struct got_patch_hunk *h;
319 size_t i;
320 int tmp;
322 STAILQ_FOREACH(h, &p->head, entries) {
323 tmp = h->old_from;
324 h->old_from = h->new_from;
325 h->new_from = tmp;
327 tmp = h->old_lines;
328 h->old_lines = h->new_lines;
329 h->new_lines = tmp;
331 tmp = h->old_nonl;
332 h->old_nonl = h->new_nonl;
333 h->new_nonl = tmp;
335 for (i = 0; i < h->len; ++i) {
336 if (*h->lines[i] == '+')
337 *h->lines[i] = '-';
338 else if (*h->lines[i] == '-')
339 *h->lines[i] = '+';
344 /*
345 * Copy data from orig starting at copypos until pos into tmp.
346 * If pos is -1, copy until EOF.
347 */
348 static const struct got_error *
349 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
351 char buf[BUFSIZ];
352 size_t len, r, w;
354 if (fseeko(orig, copypos, SEEK_SET) == -1)
355 return got_error_from_errno("fseeko");
357 while (pos == -1 || copypos < pos) {
358 len = sizeof(buf);
359 if (pos > 0)
360 len = MIN(len, (size_t)pos - copypos);
361 r = fread(buf, 1, len, orig);
362 if (r != len && ferror(orig))
363 return got_error_from_errno("fread");
364 w = fwrite(buf, 1, r, tmp);
365 if (w != r)
366 return got_error_from_errno("fwrite");
367 copypos += len;
368 if (r != len && feof(orig)) {
369 if (pos == -1)
370 return NULL;
371 return got_error(GOT_ERR_HUNK_FAILED);
374 return NULL;
377 static int linecmp(const char *, const char *, int *);
379 static const struct got_error *
380 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
382 const struct got_error *err = NULL;
383 char *line = NULL;
384 char mode = *h->lines[0];
385 size_t linesize = 0;
386 ssize_t linelen;
387 off_t match = -1;
388 int mangled = 0, match_lineno = -1;
390 for (;;) {
391 linelen = getline(&line, &linesize, orig);
392 if (linelen == -1) {
393 if (ferror(orig))
394 err = got_error_from_errno("getline");
395 else if (match == -1)
396 err = got_error(GOT_ERR_HUNK_FAILED);
397 break;
399 if (line[linelen - 1] == '\n')
400 line[linelen - 1] = '\0';
401 (*lineno)++;
403 if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
404 (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
405 (mode == '+' && *lineno == h->old_from)) {
406 match = ftello(orig);
407 if (match == -1) {
408 err = got_error_from_errno("ftello");
409 break;
411 match -= linelen;
412 match_lineno = (*lineno)-1;
415 if (*lineno >= h->old_from && match != -1) {
416 if (mangled)
417 h->ws_mangled = 1;
418 break;
422 if (err == NULL) {
423 *pos = match;
424 *lineno = match_lineno;
425 if (fseeko(orig, match, SEEK_SET) == -1)
426 err = got_error_from_errno("fseeko");
429 free(line);
430 return err;
433 static int
434 linecmp(const char *a, const char *b, int *mangled)
436 int c;
438 *mangled = 0;
439 c = strcmp(a, b);
440 if (c == 0)
441 return c;
443 *mangled = 1;
444 for (;;) {
445 while (*a == '\t' || *a == ' ' || *a == '\f')
446 a++;
447 while (*b == '\t' || *b == ' ' || *b == '\f')
448 b++;
449 if (*a == '\0' || *a != *b)
450 break;
451 a++, b++;
454 return *a - *b;
457 static const struct got_error *
458 test_hunk(FILE *orig, struct got_patch_hunk *h)
460 const struct got_error *err = NULL;
461 char *line = NULL;
462 size_t linesize = 0, i = 0;
463 ssize_t linelen;
464 int mangled;
466 for (i = 0; i < h->len; ++i) {
467 switch (*h->lines[i]) {
468 case '+':
469 continue;
470 case ' ':
471 case '-':
472 linelen = getline(&line, &linesize, orig);
473 if (linelen == -1) {
474 if (ferror(orig))
475 err = got_error_from_errno("getline");
476 else
477 err = got_error(
478 GOT_ERR_HUNK_FAILED);
479 goto done;
481 if (line[linelen - 1] == '\n')
482 line[linelen - 1] = '\0';
483 if (linecmp(h->lines[i] + 1, line, &mangled)) {
484 err = got_error(GOT_ERR_HUNK_FAILED);
485 goto done;
487 if (mangled)
488 h->ws_mangled = 1;
489 break;
493 done:
494 free(line);
495 return err;
498 static const struct got_error *
499 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
500 off_t from)
502 const struct got_error *err = NULL;
503 const char *t;
504 size_t linesize = 0, i, new = 0;
505 char *line = NULL;
506 char mode;
507 ssize_t linelen;
509 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
510 return got_error_from_errno("fseeko");
512 for (i = 0; i < h->len; ++i) {
513 switch (mode = *h->lines[i]) {
514 case '-':
515 case ' ':
516 (*lineno)++;
517 if (orig != NULL) {
518 linelen = getline(&line, &linesize, orig);
519 if (linelen == -1) {
520 err = got_error_from_errno("getline");
521 goto done;
523 if (line[linelen - 1] == '\n')
524 line[linelen - 1] = '\0';
525 t = line;
526 } else
527 t = h->lines[i] + 1;
528 if (mode == '-')
529 continue;
530 if (fprintf(tmp, "%s\n", t) < 0) {
531 err = got_error_from_errno("fprintf");
532 goto done;
534 break;
535 case '+':
536 new++;
537 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
538 err = got_error_from_errno("fprintf");
539 goto done;
541 if (new != h->new_lines || !h->new_nonl) {
542 if (fprintf(tmp, "\n") < 0) {
543 err = got_error_from_errno("fprintf");
544 goto done;
547 break;
551 done:
552 free(line);
553 return err;
556 static const struct got_error *
557 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
559 const struct got_error *err = NULL;
560 struct got_patch_hunk *h;
561 struct stat sb;
562 int lineno = 0;
563 off_t copypos, pos;
564 char *line = NULL;
565 size_t linesize = 0;
566 ssize_t linelen;
568 if (p->old == NULL) { /* create */
569 h = STAILQ_FIRST(&p->head);
570 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
571 return got_error(GOT_ERR_PATCH_MALFORMED);
572 return apply_hunk(orig, tmp, h, &lineno, 0);
575 if (fstat(fileno(orig), &sb) == -1)
576 return got_error_from_errno("fstat");
578 copypos = 0;
579 STAILQ_FOREACH(h, &p->head, entries) {
580 tryagain:
581 err = locate_hunk(orig, h, &pos, &lineno);
582 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
583 h->err = err;
584 if (err != NULL)
585 return err;
586 err = copy(tmp, orig, copypos, pos);
587 if (err != NULL)
588 return err;
589 copypos = pos;
591 err = test_hunk(orig, h);
592 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
593 /*
594 * try to apply the hunk again starting the search
595 * after the previous partial match.
596 */
597 if (fseeko(orig, pos, SEEK_SET) == -1)
598 return got_error_from_errno("fseeko");
599 linelen = getline(&line, &linesize, orig);
600 if (linelen == -1)
601 return got_error_from_errno("getline");
602 lineno++;
603 goto tryagain;
605 if (err != NULL)
606 return err;
608 if (lineno + 1 != h->old_from)
609 h->offset = lineno + 1 - h->old_from;
611 err = apply_hunk(orig, tmp, h, &lineno, pos);
612 if (err != NULL)
613 return err;
615 copypos = ftello(orig);
616 if (copypos == -1)
617 return got_error_from_errno("ftello");
620 if (p->new == NULL && sb.st_size != copypos) {
621 h = STAILQ_FIRST(&p->head);
622 h->err = got_error(GOT_ERR_HUNK_FAILED);
623 err = h->err;
624 } else if (!feof(orig))
625 err = copy(tmp, orig, copypos, -1);
627 return err;
630 static const struct got_error *
631 report_progress(struct patch_args *pa, const char *old, const char *new,
632 unsigned char status, const struct got_error *orig_error)
634 const struct got_error *err;
635 struct got_patch_hunk *h;
637 err = pa->progress_cb(pa->progress_arg, old, new, status,
638 orig_error, 0, 0, 0, 0, 0, 0, NULL);
639 if (err)
640 return err;
642 STAILQ_FOREACH(h, pa->head, entries) {
643 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
644 continue;
646 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
647 h->old_from, h->old_lines, h->new_from, h->new_lines,
648 h->offset, h->ws_mangled, h->err);
649 if (err)
650 return err;
653 return NULL;
656 static const struct got_error *
657 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
658 const char *path)
660 return report_progress(arg, path, NULL, status, NULL);
663 static const struct got_error *
664 patch_add(void *arg, unsigned char status, const char *path)
666 return report_progress(arg, NULL, path, status, NULL);
669 static const struct got_error *
670 open_blob(char **path, FILE **fp, const char *blobid,
671 struct got_repository *repo)
673 const struct got_error *err = NULL;
674 struct got_blob_object *blob = NULL;
675 struct got_object_id id, *idptr, *matched_id = NULL;
676 int fd = -1;
678 *fp = NULL;
679 *path = NULL;
681 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
682 err = got_repo_match_object_id(&matched_id, NULL, blobid,
683 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
684 repo);
685 if (err)
686 return err;
687 idptr = matched_id;
688 } else {
689 if (!got_parse_sha1_digest(id.sha1, blobid))
690 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
691 idptr = &id;
694 fd = got_opentempfd();
695 if (fd == -1) {
696 err = got_error_from_errno("got_opentempfd");
697 goto done;
700 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
701 if (err)
702 goto done;
704 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
705 if (err)
706 goto done;
708 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
709 if (err)
710 goto done;
712 done:
713 if (fd != -1 && close(fd) == -1 && err == NULL)
714 err = got_error_from_errno("close");
715 if (blob)
716 got_object_blob_close(blob);
717 if (matched_id != NULL)
718 free(matched_id);
719 if (err) {
720 if (*fp != NULL)
721 fclose(*fp);
722 if (*path != NULL)
723 unlink(*path);
724 free(*path);
725 *fp = NULL;
726 *path = NULL;
728 return err;
731 static const struct got_error *
732 prepare_merge(int *do_merge, char **apath, FILE **afile,
733 struct got_worktree *worktree, struct got_repository *repo,
734 struct got_patch *p, struct got_object_id *commit_id,
735 struct got_tree_object *tree, const char *path)
737 const struct got_error *err = NULL;
739 *do_merge = 0;
740 *apath = NULL;
741 *afile = NULL;
743 /* don't run the diff3 merge on creations/deletions */
744 if (p->old == NULL || p->new == NULL)
745 return NULL;
747 if (commit_id) {
748 struct got_object_id *id;
750 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
751 if (err)
752 return err;
753 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
754 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
755 free(id);
756 err = open_blob(apath, afile, p->blob, repo);
757 *do_merge = err == NULL;
758 } else if (*p->blob != '\0') {
759 err = open_blob(apath, afile, p->blob, repo);
760 /*
761 * ignore failures to open this blob, we might have
762 * parsed gibberish.
763 */
764 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
765 err->code != GOT_ERR_NO_OBJ)
766 return err;
767 *do_merge = err == NULL;
768 err = NULL;
771 return err;
774 static const struct got_error *
775 apply_patch(int *overlapcnt, struct got_worktree *worktree,
776 struct got_repository *repo, struct got_fileindex *fileindex,
777 const char *old, const char *new, struct got_patch *p, int nop,
778 int reverse, struct got_object_id *commit_id,
779 struct got_tree_object *tree, struct patch_args *pa,
780 got_cancel_cb cancel_cb, void *cancel_arg)
782 const struct got_error *err = NULL;
783 struct stat sb;
784 int do_merge = 0, file_renamed = 0;
785 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
786 char *oldpath = NULL, *newpath = NULL;
787 char *tmppath = NULL, *template = NULL, *parent = NULL;
788 char *apath = NULL, *mergepath = NULL;
789 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
790 int outfd;
791 const char *outpath;
792 mode_t mode = GOT_DEFAULT_FILE_MODE;
794 *overlapcnt = 0;
796 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
797 commit_id, tree, old);
798 if (err)
799 return err;
801 if (reverse && !do_merge)
802 reverse_patch(p);
804 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
805 old) == -1) {
806 err = got_error_from_errno("asprintf");
807 goto done;
810 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
811 new) == -1) {
812 err = got_error_from_errno("asprintf");
813 goto done;
816 file_renamed = strcmp(oldpath, newpath);
818 if (asprintf(&template, "%s/got-patch",
819 got_worktree_get_root_path(worktree)) == -1) {
820 err = got_error_from_errno(template);
821 goto done;
824 if (p->old != NULL) {
825 if ((oldfile = fopen(oldpath, "r")) == NULL) {
826 err = got_error_from_errno2("open", oldpath);
827 goto done;
829 if (fstat(fileno(oldfile), &sb) == -1) {
830 err = got_error_from_errno2("fstat", oldpath);
831 goto done;
833 mode = sb.st_mode;
836 err = got_opentemp_named(&tmppath, &tmpfile, template);
837 if (err)
838 goto done;
839 outpath = tmppath;
840 outfd = fileno(tmpfile);
841 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
842 if (err)
843 goto done;
845 if (do_merge) {
846 const char *type, *id;
848 if (fseeko(afile, 0, SEEK_SET) == -1 ||
849 fseeko(oldfile, 0, SEEK_SET) == -1 ||
850 fseeko(tmpfile, 0, SEEK_SET) == -1) {
851 err = got_error_from_errno("fseeko");
852 goto done;
855 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
856 err = got_error_from_errno("asprintf");
857 oldlabel = NULL;
858 goto done;
861 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
862 err = got_error_from_errno("asprintf");
863 newlabel = NULL;
864 goto done;
867 if (*p->cid != '\0') {
868 type = "commit";
869 id = p->cid;
870 } else {
871 type = "blob";
872 id = p->blob;
875 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
876 err = got_error_from_errno("asprintf");
877 anclabel = NULL;
878 goto done;
881 if (reverse) {
882 char *s;
883 FILE *t;
885 s = anclabel;
886 anclabel = newlabel;
887 newlabel = s;
889 t = afile;
890 afile = tmpfile;
891 tmpfile = t;
894 err = got_opentemp_named(&mergepath, &mergefile, template);
895 if (err)
896 goto done;
897 outpath = mergepath;
898 outfd = fileno(mergefile);
900 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
901 oldfile, oldlabel, anclabel, newlabel,
902 GOT_DIFF_ALGORITHM_PATIENCE);
903 if (err)
904 goto done;
907 if (nop)
908 goto done;
910 if (p->old != NULL && p->new == NULL) {
911 err = got_worktree_patch_schedule_rm(old, repo, worktree,
912 fileindex, patch_delete, pa);
913 goto done;
916 if (fchmod(outfd, mode) == -1) {
917 err = got_error_from_errno2("chmod", tmppath);
918 goto done;
921 if (rename(outpath, newpath) == -1) {
922 if (errno != ENOENT) {
923 err = got_error_from_errno3("rename", outpath,
924 newpath);
925 goto done;
928 err = got_path_dirname(&parent, newpath);
929 if (err != NULL)
930 goto done;
931 err = got_path_mkdir(parent);
932 if (err != NULL)
933 goto done;
934 if (rename(outpath, newpath) == -1) {
935 err = got_error_from_errno3("rename", outpath,
936 newpath);
937 goto done;
941 if (file_renamed) {
942 err = got_worktree_patch_schedule_rm(old, repo, worktree,
943 fileindex, patch_delete, pa);
944 if (err == NULL)
945 err = got_worktree_patch_schedule_add(new, repo,
946 worktree, fileindex, patch_add,
947 pa);
948 if (err)
949 unlink(newpath);
950 } else if (p->old == NULL) {
951 err = got_worktree_patch_schedule_add(new, repo, worktree,
952 fileindex, patch_add, pa);
953 if (err)
954 unlink(newpath);
955 } else if (*overlapcnt != 0)
956 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
957 else if (do_merge)
958 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
959 else
960 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
962 done:
963 free(parent);
964 free(template);
966 if (tmppath != NULL)
967 unlink(tmppath);
968 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
969 err = got_error_from_errno("fclose");
970 free(tmppath);
972 free(oldpath);
973 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
974 err = got_error_from_errno("fclose");
976 if (apath != NULL)
977 unlink(apath);
978 if (afile != NULL && fclose(afile) == EOF && err == NULL)
979 err = got_error_from_errno("fclose");
980 free(apath);
982 if (mergepath != NULL)
983 unlink(mergepath);
984 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
985 err = got_error_from_errno("fclose");
986 free(mergepath);
988 free(newpath);
989 free(oldlabel);
990 free(newlabel);
991 free(anclabel);
992 return err;
995 const struct got_error *
996 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
997 int nop, int strip, int reverse, struct got_object_id *commit_id,
998 got_patch_progress_cb progress_cb, void *progress_arg,
999 got_cancel_cb cancel_cb, void *cancel_arg)
1001 const struct got_error *err = NULL, *complete_err = NULL;
1002 struct got_fileindex *fileindex = NULL;
1003 struct got_commit_object *commit = NULL;
1004 struct got_tree_object *tree = NULL;
1005 char *fileindex_path = NULL;
1006 char *oldpath, *newpath;
1007 struct imsgbuf *ibuf;
1008 int imsg_fds[2] = {-1, -1};
1009 int overlapcnt, done = 0, failed = 0;
1010 pid_t pid;
1012 ibuf = calloc(1, sizeof(*ibuf));
1013 if (ibuf == NULL) {
1014 err = got_error_from_errno("calloc");
1015 goto done;
1018 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1019 err = got_error_from_errno("socketpair");
1020 goto done;
1023 pid = fork();
1024 if (pid == -1) {
1025 err = got_error_from_errno("fork");
1026 goto done;
1027 } else if (pid == 0) {
1028 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1029 NULL);
1030 /* not reached */
1033 if (close(imsg_fds[1]) == -1) {
1034 err = got_error_from_errno("close");
1035 goto done;
1037 imsg_fds[1] = -1;
1038 imsg_init(ibuf, imsg_fds[0]);
1040 err = send_patch(ibuf, fd);
1041 fd = -1;
1042 if (err)
1043 goto done;
1045 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1046 worktree);
1047 if (err)
1048 goto done;
1050 if (commit_id) {
1051 err = got_object_open_as_commit(&commit, repo, commit_id);
1052 if (err)
1053 goto done;
1055 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1056 if (err)
1057 goto done;
1060 while (!done && err == NULL) {
1061 struct got_patch p;
1062 struct patch_args pa;
1064 pa.progress_cb = progress_cb;
1065 pa.progress_arg = progress_arg;
1066 pa.head = &p.head;
1068 err = recv_patch(ibuf, &done, &p, strip);
1069 if (err || done)
1070 break;
1072 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1073 &newpath, worktree, repo, fileindex);
1074 if (err == NULL)
1075 err = apply_patch(&overlapcnt, worktree, repo,
1076 fileindex, oldpath, newpath, &p, nop, reverse,
1077 commit_id, tree, &pa, cancel_cb, cancel_arg);
1078 if (err != NULL) {
1079 failed = 1;
1080 /* recoverable errors */
1081 if (err->code == GOT_ERR_FILE_STATUS ||
1082 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1083 err = report_progress(&pa, p.old, p.new,
1084 GOT_STATUS_CANNOT_UPDATE, err);
1085 else if (err->code == GOT_ERR_HUNK_FAILED)
1086 err = report_progress(&pa, p.old, p.new,
1087 GOT_STATUS_CANNOT_UPDATE, NULL);
1089 if (overlapcnt != 0)
1090 failed = 1;
1092 free(oldpath);
1093 free(newpath);
1094 patch_free(&p);
1096 if (err)
1097 break;
1100 done:
1101 if (fileindex != NULL)
1102 complete_err = got_worktree_patch_complete(fileindex,
1103 fileindex_path);
1104 if (complete_err && err == NULL)
1105 err = complete_err;
1106 free(fileindex_path);
1107 if (tree)
1108 got_object_tree_close(tree);
1109 if (commit)
1110 got_object_commit_close(commit);
1111 if (fd != -1 && close(fd) == -1 && err == NULL)
1112 err = got_error_from_errno("close");
1113 if (ibuf != NULL)
1114 imsg_clear(ibuf);
1115 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1116 err = got_error_from_errno("close");
1117 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1118 err = got_error_from_errno("close");
1119 if (err == NULL && failed)
1120 err = got_error(GOT_ERR_PATCH_FAILED);
1121 return err;