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 int xbit;
76 char *old;
77 char *new;
78 char cid[41];
79 char blob[41];
80 struct got_patch_hunk_head head;
81 };
83 struct patch_args {
84 got_patch_progress_cb progress_cb;
85 void *progress_arg;
86 struct got_patch_hunk_head *head;
87 };
89 static const struct got_error *
90 send_patch(struct imsgbuf *ibuf, int fd)
91 {
92 const struct got_error *err = NULL;
94 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
95 NULL, 0) == -1) {
96 err = got_error_from_errno(
97 "imsg_compose GOT_IMSG_PATCH_FILE");
98 close(fd);
99 return err;
102 return got_privsep_flush_imsg(ibuf);
105 static void
106 patch_free(struct got_patch *p)
108 struct got_patch_hunk *h;
109 size_t i;
111 while (!STAILQ_EMPTY(&p->head)) {
112 h = STAILQ_FIRST(&p->head);
113 STAILQ_REMOVE_HEAD(&p->head, entries);
115 for (i = 0; i < h->len; ++i)
116 free(h->lines[i]);
117 free(h->lines);
118 free(h);
121 free(p->new);
122 free(p->old);
124 memset(p, 0, sizeof(*p));
125 STAILQ_INIT(&p->head);
128 static const struct got_error *
129 pushline(struct got_patch_hunk *h, const char *line)
131 void *t;
132 size_t newcap;
134 if (h->len == h->cap) {
135 if ((newcap = h->cap * 1.5) == 0)
136 newcap = 16;
137 t = recallocarray(h->lines, h->cap, newcap,
138 sizeof(h->lines[0]));
139 if (t == NULL)
140 return got_error_from_errno("recallocarray");
141 h->lines = t;
142 h->cap = newcap;
145 if ((t = strdup(line)) == NULL)
146 return got_error_from_errno("strdup");
148 h->lines[h->len++] = t;
149 return NULL;
152 static const struct got_error *
153 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
155 const struct got_error *err = NULL;
156 struct imsg imsg;
157 struct got_imsg_patch_hunk hdr;
158 struct got_imsg_patch patch;
159 struct got_patch_hunk *h = NULL;
160 size_t datalen;
161 int lastmode = -1;
163 memset(p, 0, sizeof(*p));
164 STAILQ_INIT(&p->head);
166 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
167 if (err)
168 return err;
169 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
170 *done = 1;
171 goto done;
173 if (imsg.hdr.type != GOT_IMSG_PATCH) {
174 err = got_error(GOT_ERR_PRIVSEP_MSG);
175 goto done;
177 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
178 if (datalen != sizeof(patch)) {
179 err = got_error(GOT_ERR_PRIVSEP_LEN);
180 goto done;
182 memcpy(&patch, imsg.data, sizeof(patch));
184 if (patch.old[sizeof(patch.old)-1] != '\0' ||
185 patch.new[sizeof(patch.new)-1] != '\0' ||
186 patch.cid[sizeof(patch.cid)-1] != '\0' ||
187 patch.blob[sizeof(patch.blob)-1] != '\0') {
188 err = got_error(GOT_ERR_PRIVSEP_LEN);
189 goto done;
192 if (*patch.cid != '\0')
193 strlcpy(p->cid, patch.cid, sizeof(p->cid));
195 if (*patch.blob != '\0')
196 strlcpy(p->blob, patch.blob, sizeof(p->blob));
198 p->xbit = patch.xbit;
200 /* automatically set strip=1 for git-style diffs */
201 if (strip == -1 && patch.git &&
202 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
203 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
204 strip = 1;
206 /* prefer the new name if not /dev/null for not git-style diffs */
207 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
208 err = got_path_strip(&p->old, patch.new, strip);
209 if (err)
210 goto done;
211 } else if (*patch.old != '\0') {
212 err = got_path_strip(&p->old, patch.old, strip);
213 if (err)
214 goto done;
217 if (*patch.new != '\0') {
218 err = got_path_strip(&p->new, patch.new, strip);
219 if (err)
220 goto done;
223 if (p->old == NULL && p->new == NULL) {
224 err = got_error(GOT_ERR_PATCH_MALFORMED);
225 goto done;
228 imsg_free(&imsg);
230 for (;;) {
231 char *t;
233 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
234 if (err) {
235 patch_free(p);
236 return err;
239 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
240 switch (imsg.hdr.type) {
241 case GOT_IMSG_PATCH_DONE:
242 if (h != NULL && h->len == 0)
243 err = got_error(GOT_ERR_PATCH_MALFORMED);
244 goto done;
245 case GOT_IMSG_PATCH_HUNK:
246 if (h != NULL &&
247 (h->len == 0 || h->old_nonl || h->new_nonl)) {
248 err = got_error(GOT_ERR_PATCH_MALFORMED);
249 goto done;
251 lastmode = -1;
252 if (datalen != sizeof(hdr)) {
253 err = got_error(GOT_ERR_PRIVSEP_LEN);
254 goto done;
256 memcpy(&hdr, imsg.data, sizeof(hdr));
257 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
258 err = got_error(GOT_ERR_PRIVSEP_LEN);
259 goto done;
261 if ((h = calloc(1, sizeof(*h))) == NULL) {
262 err = got_error_from_errno("calloc");
263 goto done;
265 h->old_from = hdr.oldfrom;
266 h->old_lines = hdr.oldlines;
267 h->new_from = hdr.newfrom;
268 h->new_lines = hdr.newlines;
269 STAILQ_INSERT_TAIL(&p->head, h, entries);
270 break;
271 case GOT_IMSG_PATCH_LINE:
272 if (h == NULL) {
273 err = got_error(GOT_ERR_PRIVSEP_MSG);
274 goto done;
276 t = imsg.data;
277 /* at least one char */
278 if (datalen < 2 || t[datalen-1] != '\0') {
279 err = got_error(GOT_ERR_PRIVSEP_MSG);
280 goto done;
282 if (*t != ' ' && *t != '-' && *t != '+' &&
283 *t != '\\') {
284 err = got_error(GOT_ERR_PRIVSEP_MSG);
285 goto done;
288 if (*t != '\\')
289 err = pushline(h, t);
290 else if (lastmode == '-')
291 h->old_nonl = 1;
292 else if (lastmode == '+')
293 h->new_nonl = 1;
294 else
295 err = got_error(GOT_ERR_PATCH_MALFORMED);
297 if (err)
298 goto done;
300 lastmode = *t;
301 break;
302 default:
303 err = got_error(GOT_ERR_PRIVSEP_MSG);
304 goto done;
307 imsg_free(&imsg);
310 done:
311 if (err)
312 patch_free(p);
314 imsg_free(&imsg);
315 return err;
318 static void
319 reverse_patch(struct got_patch *p)
321 struct got_patch_hunk *h;
322 size_t i;
323 int tmp;
325 STAILQ_FOREACH(h, &p->head, entries) {
326 tmp = h->old_from;
327 h->old_from = h->new_from;
328 h->new_from = tmp;
330 tmp = h->old_lines;
331 h->old_lines = h->new_lines;
332 h->new_lines = tmp;
334 tmp = h->old_nonl;
335 h->old_nonl = h->new_nonl;
336 h->new_nonl = tmp;
338 for (i = 0; i < h->len; ++i) {
339 if (*h->lines[i] == '+')
340 *h->lines[i] = '-';
341 else if (*h->lines[i] == '-')
342 *h->lines[i] = '+';
347 /*
348 * Copy data from orig starting at copypos until pos into tmp.
349 * If pos is -1, copy until EOF.
350 */
351 static const struct got_error *
352 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
354 char buf[BUFSIZ];
355 size_t len, r, w;
357 if (fseeko(orig, copypos, SEEK_SET) == -1)
358 return got_error_from_errno("fseeko");
360 while (pos == -1 || copypos < pos) {
361 len = sizeof(buf);
362 if (pos > 0)
363 len = MIN(len, (size_t)pos - copypos);
364 r = fread(buf, 1, len, orig);
365 if (r != len && ferror(orig))
366 return got_error_from_errno("fread");
367 w = fwrite(buf, 1, r, tmp);
368 if (w != r)
369 return got_error_from_errno("fwrite");
370 copypos += len;
371 if (r != len && feof(orig)) {
372 if (pos == -1)
373 return NULL;
374 return got_error(GOT_ERR_HUNK_FAILED);
377 return NULL;
380 static int linecmp(const char *, const char *, int *);
382 static const struct got_error *
383 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
385 const struct got_error *err = NULL;
386 char *line = NULL;
387 char mode = *h->lines[0];
388 size_t linesize = 0;
389 ssize_t linelen;
390 off_t match = -1;
391 int mangled = 0, match_lineno = -1;
393 for (;;) {
394 linelen = getline(&line, &linesize, orig);
395 if (linelen == -1) {
396 if (ferror(orig))
397 err = got_error_from_errno("getline");
398 else if (match == -1)
399 err = got_error(GOT_ERR_HUNK_FAILED);
400 break;
402 if (line[linelen - 1] == '\n')
403 line[linelen - 1] = '\0';
404 (*lineno)++;
406 if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
407 (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
408 (mode == '+' && *lineno == h->old_from)) {
409 match = ftello(orig);
410 if (match == -1) {
411 err = got_error_from_errno("ftello");
412 break;
414 match -= linelen;
415 match_lineno = (*lineno)-1;
418 if (*lineno >= h->old_from && match != -1) {
419 if (mangled)
420 h->ws_mangled = 1;
421 break;
425 if (err == NULL) {
426 *pos = match;
427 *lineno = match_lineno;
428 if (fseeko(orig, match, SEEK_SET) == -1)
429 err = got_error_from_errno("fseeko");
432 free(line);
433 return err;
436 static int
437 linecmp(const char *a, const char *b, int *mangled)
439 int c;
441 *mangled = 0;
442 c = strcmp(a, b);
443 if (c == 0)
444 return c;
446 *mangled = 1;
447 for (;;) {
448 while (*a == '\t' || *a == ' ' || *a == '\f')
449 a++;
450 while (*b == '\t' || *b == ' ' || *b == '\f')
451 b++;
452 if (*a == '\0' || *a != *b)
453 break;
454 a++, b++;
457 return *a - *b;
460 static const struct got_error *
461 test_hunk(FILE *orig, struct got_patch_hunk *h)
463 const struct got_error *err = NULL;
464 char *line = NULL;
465 size_t linesize = 0, i = 0;
466 ssize_t linelen;
467 int mangled;
469 for (i = 0; i < h->len; ++i) {
470 switch (*h->lines[i]) {
471 case '+':
472 continue;
473 case ' ':
474 case '-':
475 linelen = getline(&line, &linesize, orig);
476 if (linelen == -1) {
477 if (ferror(orig))
478 err = got_error_from_errno("getline");
479 else
480 err = got_error(
481 GOT_ERR_HUNK_FAILED);
482 goto done;
484 if (line[linelen - 1] == '\n')
485 line[linelen - 1] = '\0';
486 if (linecmp(h->lines[i] + 1, line, &mangled)) {
487 err = got_error(GOT_ERR_HUNK_FAILED);
488 goto done;
490 if (mangled)
491 h->ws_mangled = 1;
492 break;
496 done:
497 free(line);
498 return err;
501 static const struct got_error *
502 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
503 off_t from)
505 const struct got_error *err = NULL;
506 const char *t;
507 size_t linesize = 0, i, new = 0;
508 char *line = NULL;
509 char mode;
510 ssize_t linelen;
512 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
513 return got_error_from_errno("fseeko");
515 for (i = 0; i < h->len; ++i) {
516 switch (mode = *h->lines[i]) {
517 case '-':
518 case ' ':
519 (*lineno)++;
520 if (orig != NULL) {
521 linelen = getline(&line, &linesize, orig);
522 if (linelen == -1) {
523 err = got_error_from_errno("getline");
524 goto done;
526 if (line[linelen - 1] == '\n')
527 line[linelen - 1] = '\0';
528 t = line;
529 } else
530 t = h->lines[i] + 1;
531 if (mode == '-')
532 continue;
533 if (fprintf(tmp, "%s\n", t) < 0) {
534 err = got_error_from_errno("fprintf");
535 goto done;
537 break;
538 case '+':
539 new++;
540 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
541 err = got_error_from_errno("fprintf");
542 goto done;
544 if (new != h->new_lines || !h->new_nonl) {
545 if (fprintf(tmp, "\n") < 0) {
546 err = got_error_from_errno("fprintf");
547 goto done;
550 break;
554 done:
555 free(line);
556 return err;
559 static const struct got_error *
560 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
562 const struct got_error *err = NULL;
563 struct got_patch_hunk *h;
564 struct stat sb;
565 int lineno = 0;
566 off_t copypos, pos;
567 char *line = NULL;
568 size_t linesize = 0;
569 ssize_t linelen;
571 if (p->old == NULL) { /* create */
572 h = STAILQ_FIRST(&p->head);
573 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
574 return got_error(GOT_ERR_PATCH_MALFORMED);
575 return apply_hunk(orig, tmp, h, &lineno, 0);
578 if (fstat(fileno(orig), &sb) == -1)
579 return got_error_from_errno("fstat");
581 copypos = 0;
582 STAILQ_FOREACH(h, &p->head, entries) {
583 tryagain:
584 err = locate_hunk(orig, h, &pos, &lineno);
585 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
586 h->err = err;
587 if (err != NULL)
588 return err;
589 err = copy(tmp, orig, copypos, pos);
590 if (err != NULL)
591 return err;
592 copypos = pos;
594 err = test_hunk(orig, h);
595 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
596 /*
597 * try to apply the hunk again starting the search
598 * after the previous partial match.
599 */
600 if (fseeko(orig, pos, SEEK_SET) == -1)
601 return got_error_from_errno("fseeko");
602 linelen = getline(&line, &linesize, orig);
603 if (linelen == -1)
604 return got_error_from_errno("getline");
605 lineno++;
606 goto tryagain;
608 if (err != NULL)
609 return err;
611 if (lineno + 1 != h->old_from)
612 h->offset = lineno + 1 - h->old_from;
614 err = apply_hunk(orig, tmp, h, &lineno, pos);
615 if (err != NULL)
616 return err;
618 copypos = ftello(orig);
619 if (copypos == -1)
620 return got_error_from_errno("ftello");
623 if (p->new == NULL && sb.st_size != copypos) {
624 h = STAILQ_FIRST(&p->head);
625 h->err = got_error(GOT_ERR_HUNK_FAILED);
626 err = h->err;
627 } else if (!feof(orig))
628 err = copy(tmp, orig, copypos, -1);
630 return err;
633 static const struct got_error *
634 report_progress(struct patch_args *pa, const char *old, const char *new,
635 unsigned char status, const struct got_error *orig_error)
637 const struct got_error *err;
638 struct got_patch_hunk *h;
640 err = pa->progress_cb(pa->progress_arg, old, new, status,
641 orig_error, 0, 0, 0, 0, 0, 0, NULL);
642 if (err)
643 return err;
645 STAILQ_FOREACH(h, pa->head, entries) {
646 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
647 continue;
649 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
650 h->old_from, h->old_lines, h->new_from, h->new_lines,
651 h->offset, h->ws_mangled, h->err);
652 if (err)
653 return err;
656 return NULL;
659 static const struct got_error *
660 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
661 const char *path)
663 return report_progress(arg, path, NULL, status, NULL);
666 static const struct got_error *
667 patch_add(void *arg, unsigned char status, const char *path)
669 return report_progress(arg, NULL, path, status, NULL);
672 static const struct got_error *
673 open_blob(char **path, FILE **fp, const char *blobid,
674 struct got_repository *repo)
676 const struct got_error *err = NULL;
677 struct got_blob_object *blob = NULL;
678 struct got_object_id id, *idptr, *matched_id = NULL;
679 int fd = -1;
681 *fp = NULL;
682 *path = NULL;
684 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
685 err = got_repo_match_object_id(&matched_id, NULL, blobid,
686 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
687 repo);
688 if (err)
689 return err;
690 idptr = matched_id;
691 } else {
692 if (!got_parse_sha1_digest(id.sha1, blobid))
693 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
694 idptr = &id;
697 fd = got_opentempfd();
698 if (fd == -1) {
699 err = got_error_from_errno("got_opentempfd");
700 goto done;
703 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
704 if (err)
705 goto done;
707 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
708 if (err)
709 goto done;
711 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
712 if (err)
713 goto done;
715 done:
716 if (fd != -1 && close(fd) == -1 && err == NULL)
717 err = got_error_from_errno("close");
718 if (blob)
719 got_object_blob_close(blob);
720 if (matched_id != NULL)
721 free(matched_id);
722 if (err) {
723 if (*fp != NULL)
724 fclose(*fp);
725 if (*path != NULL)
726 unlink(*path);
727 free(*path);
728 *fp = NULL;
729 *path = NULL;
731 return err;
734 static const struct got_error *
735 prepare_merge(int *do_merge, char **apath, FILE **afile,
736 struct got_worktree *worktree, struct got_repository *repo,
737 struct got_patch *p, struct got_object_id *commit_id,
738 struct got_tree_object *tree, const char *path)
740 const struct got_error *err = NULL;
742 *do_merge = 0;
743 *apath = NULL;
744 *afile = NULL;
746 /* don't run the diff3 merge on creations/deletions */
747 if (p->old == NULL || p->new == NULL)
748 return NULL;
750 if (commit_id) {
751 struct got_object_id *id;
753 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
754 if (err)
755 return err;
756 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
757 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
758 free(id);
759 err = open_blob(apath, afile, p->blob, repo);
760 *do_merge = err == NULL;
761 } else if (*p->blob != '\0') {
762 err = open_blob(apath, afile, p->blob, repo);
763 /*
764 * ignore failures to open this blob, we might have
765 * parsed gibberish.
766 */
767 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
768 err->code != GOT_ERR_NO_OBJ)
769 return err;
770 *do_merge = err == NULL;
771 err = NULL;
774 return err;
777 static const struct got_error *
778 apply_patch(int *overlapcnt, struct got_worktree *worktree,
779 struct got_repository *repo, struct got_fileindex *fileindex,
780 const char *old, const char *new, struct got_patch *p, int nop,
781 int reverse, struct got_object_id *commit_id,
782 struct got_tree_object *tree, struct patch_args *pa,
783 got_cancel_cb cancel_cb, void *cancel_arg)
785 const struct got_error *err = NULL;
786 struct stat sb;
787 int do_merge = 0, file_renamed = 0;
788 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
789 char *oldpath = NULL, *newpath = NULL;
790 char *tmppath = NULL, *template = NULL, *parent = NULL;
791 char *apath = NULL, *mergepath = NULL;
792 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
793 int outfd;
794 const char *outpath;
795 mode_t mode = GOT_DEFAULT_FILE_MODE;
797 *overlapcnt = 0;
799 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
800 commit_id, tree, old);
801 if (err)
802 return err;
804 if (reverse && !do_merge)
805 reverse_patch(p);
807 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
808 old) == -1) {
809 err = got_error_from_errno("asprintf");
810 goto done;
813 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
814 new) == -1) {
815 err = got_error_from_errno("asprintf");
816 goto done;
819 file_renamed = strcmp(oldpath, newpath);
821 if (asprintf(&template, "%s/got-patch",
822 got_worktree_get_root_path(worktree)) == -1) {
823 err = got_error_from_errno(template);
824 goto done;
827 if (p->old != NULL) {
828 if ((oldfile = fopen(oldpath, "r")) == NULL) {
829 err = got_error_from_errno2("open", oldpath);
830 goto done;
832 if (fstat(fileno(oldfile), &sb) == -1) {
833 err = got_error_from_errno2("fstat", oldpath);
834 goto done;
836 mode = sb.st_mode;
837 } else if (p->xbit)
838 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
840 err = got_opentemp_named(&tmppath, &tmpfile, template);
841 if (err)
842 goto done;
843 outpath = tmppath;
844 outfd = fileno(tmpfile);
845 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
846 if (err)
847 goto done;
849 if (do_merge) {
850 const char *type, *id;
852 if (fseeko(afile, 0, SEEK_SET) == -1 ||
853 fseeko(oldfile, 0, SEEK_SET) == -1 ||
854 fseeko(tmpfile, 0, SEEK_SET) == -1) {
855 err = got_error_from_errno("fseeko");
856 goto done;
859 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
860 err = got_error_from_errno("asprintf");
861 oldlabel = NULL;
862 goto done;
865 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
866 err = got_error_from_errno("asprintf");
867 newlabel = NULL;
868 goto done;
871 if (*p->cid != '\0') {
872 type = "commit";
873 id = p->cid;
874 } else {
875 type = "blob";
876 id = p->blob;
879 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
880 err = got_error_from_errno("asprintf");
881 anclabel = NULL;
882 goto done;
885 if (reverse) {
886 char *s;
887 FILE *t;
889 s = anclabel;
890 anclabel = newlabel;
891 newlabel = s;
893 t = afile;
894 afile = tmpfile;
895 tmpfile = t;
898 err = got_opentemp_named(&mergepath, &mergefile, template);
899 if (err)
900 goto done;
901 outpath = mergepath;
902 outfd = fileno(mergefile);
904 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
905 oldfile, oldlabel, anclabel, newlabel,
906 GOT_DIFF_ALGORITHM_PATIENCE);
907 if (err)
908 goto done;
911 if (nop)
912 goto done;
914 if (p->old != NULL && p->new == NULL) {
915 err = got_worktree_patch_schedule_rm(old, repo, worktree,
916 fileindex, patch_delete, pa);
917 goto done;
920 if (fchmod(outfd, mode) == -1) {
921 err = got_error_from_errno2("chmod", tmppath);
922 goto done;
925 if (rename(outpath, newpath) == -1) {
926 if (errno != ENOENT) {
927 err = got_error_from_errno3("rename", outpath,
928 newpath);
929 goto done;
932 err = got_path_dirname(&parent, newpath);
933 if (err != NULL)
934 goto done;
935 err = got_path_mkdir(parent);
936 if (err != NULL)
937 goto done;
938 if (rename(outpath, newpath) == -1) {
939 err = got_error_from_errno3("rename", outpath,
940 newpath);
941 goto done;
945 if (file_renamed) {
946 err = got_worktree_patch_schedule_rm(old, repo, worktree,
947 fileindex, patch_delete, pa);
948 if (err == NULL)
949 err = got_worktree_patch_schedule_add(new, repo,
950 worktree, fileindex, patch_add,
951 pa);
952 if (err)
953 unlink(newpath);
954 } else if (p->old == NULL) {
955 err = got_worktree_patch_schedule_add(new, repo, worktree,
956 fileindex, patch_add, pa);
957 if (err)
958 unlink(newpath);
959 } else if (*overlapcnt != 0)
960 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
961 else if (do_merge)
962 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
963 else
964 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
966 done:
967 free(parent);
968 free(template);
970 if (tmppath != NULL)
971 unlink(tmppath);
972 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
973 err = got_error_from_errno("fclose");
974 free(tmppath);
976 free(oldpath);
977 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
978 err = got_error_from_errno("fclose");
980 if (apath != NULL)
981 unlink(apath);
982 if (afile != NULL && fclose(afile) == EOF && err == NULL)
983 err = got_error_from_errno("fclose");
984 free(apath);
986 if (mergepath != NULL)
987 unlink(mergepath);
988 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
989 err = got_error_from_errno("fclose");
990 free(mergepath);
992 free(newpath);
993 free(oldlabel);
994 free(newlabel);
995 free(anclabel);
996 return err;
999 const struct got_error *
1000 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1001 int nop, int strip, int reverse, struct got_object_id *commit_id,
1002 got_patch_progress_cb progress_cb, void *progress_arg,
1003 got_cancel_cb cancel_cb, void *cancel_arg)
1005 const struct got_error *err = NULL, *complete_err = NULL;
1006 struct got_fileindex *fileindex = NULL;
1007 struct got_commit_object *commit = NULL;
1008 struct got_tree_object *tree = NULL;
1009 char *fileindex_path = NULL;
1010 char *oldpath, *newpath;
1011 struct imsgbuf *ibuf;
1012 int imsg_fds[2] = {-1, -1};
1013 int overlapcnt, done = 0, failed = 0;
1014 pid_t pid;
1016 ibuf = calloc(1, sizeof(*ibuf));
1017 if (ibuf == NULL) {
1018 err = got_error_from_errno("calloc");
1019 goto done;
1022 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1023 err = got_error_from_errno("socketpair");
1024 goto done;
1027 pid = fork();
1028 if (pid == -1) {
1029 err = got_error_from_errno("fork");
1030 goto done;
1031 } else if (pid == 0) {
1032 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1033 NULL);
1034 /* not reached */
1037 if (close(imsg_fds[1]) == -1) {
1038 err = got_error_from_errno("close");
1039 goto done;
1041 imsg_fds[1] = -1;
1042 imsg_init(ibuf, imsg_fds[0]);
1044 err = send_patch(ibuf, fd);
1045 fd = -1;
1046 if (err)
1047 goto done;
1049 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1050 worktree);
1051 if (err)
1052 goto done;
1054 if (commit_id) {
1055 err = got_object_open_as_commit(&commit, repo, commit_id);
1056 if (err)
1057 goto done;
1059 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1060 if (err)
1061 goto done;
1064 while (!done && err == NULL) {
1065 struct got_patch p;
1066 struct patch_args pa;
1068 pa.progress_cb = progress_cb;
1069 pa.progress_arg = progress_arg;
1070 pa.head = &p.head;
1072 err = recv_patch(ibuf, &done, &p, strip);
1073 if (err || done)
1074 break;
1076 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1077 &newpath, worktree, repo, fileindex);
1078 if (err == NULL)
1079 err = apply_patch(&overlapcnt, worktree, repo,
1080 fileindex, oldpath, newpath, &p, nop, reverse,
1081 commit_id, tree, &pa, cancel_cb, cancel_arg);
1082 if (err != NULL) {
1083 failed = 1;
1084 /* recoverable errors */
1085 if (err->code == GOT_ERR_FILE_STATUS ||
1086 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1087 err = report_progress(&pa, p.old, p.new,
1088 GOT_STATUS_CANNOT_UPDATE, err);
1089 else if (err->code == GOT_ERR_HUNK_FAILED)
1090 err = report_progress(&pa, p.old, p.new,
1091 GOT_STATUS_CANNOT_UPDATE, NULL);
1093 if (overlapcnt != 0)
1094 failed = 1;
1096 free(oldpath);
1097 free(newpath);
1098 patch_free(&p);
1100 if (err)
1101 break;
1104 done:
1105 if (fileindex != NULL)
1106 complete_err = got_worktree_patch_complete(fileindex,
1107 fileindex_path);
1108 if (complete_err && err == NULL)
1109 err = complete_err;
1110 free(fileindex_path);
1111 if (tree)
1112 got_object_tree_close(tree);
1113 if (commit)
1114 got_object_commit_close(commit);
1115 if (fd != -1 && close(fd) == -1 && err == NULL)
1116 err = got_error_from_errno("close");
1117 if (ibuf != NULL)
1118 imsg_clear(ibuf);
1119 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1120 err = got_error_from_errno("close");
1121 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1122 err = got_error_from_errno("close");
1123 if (err == NULL && failed)
1124 err = got_error(GOT_ERR_PATCH_FAILED);
1125 return err;