tog: group state assignments and zap dup repo assignment
[got-portable.git] / lib / patch.c
blob9185475a2eaa2216fdc69b925696359ca53b2e12
1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
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.
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.
16 * Apply patches.
18 * Things that we may want to support:
19 * + support indented patches?
20 * + support other kinds of patches?
22 #include "got_compat.h"
24 #include <sys/types.h>
25 #include <sys/queue.h>
26 #include <sys/socket.h>
27 #include <sys/stat.h>
28 #include <sys/uio.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_path.h"
42 #include "got_reference.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_repository.h"
46 #include "got_opentemp.h"
47 #include "got_patch.h"
48 #include "got_diff.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_diff.h"
52 #include "got_lib_hash.h"
53 #include "got_lib_object.h"
54 #include "got_lib_privsep.h"
56 #ifndef MIN
57 #define MIN(a, b) ((a) < (b) ? (a) : (b))
58 #endif
60 struct got_patch_line {
61 char mode;
62 char *line;
63 size_t len;
66 struct got_patch_hunk {
67 STAILQ_ENTRY(got_patch_hunk) entries;
68 const struct got_error *err;
69 int ws_mangled;
70 int offset;
71 int old_nonl;
72 int new_nonl;
73 int old_from;
74 int old_lines;
75 int new_from;
76 int new_lines;
77 size_t len;
78 size_t cap;
79 struct got_patch_line *lines;
82 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
83 struct got_patch {
84 int xbit;
85 char *old;
86 char *new;
87 char cid[GOT_HASH_DIGEST_STRING_MAXLEN];
88 char blob[GOT_HASH_DIGEST_STRING_MAXLEN];
89 struct got_patch_hunk_head head;
92 struct patch_args {
93 got_patch_progress_cb progress_cb;
94 void *progress_arg;
95 struct got_patch_hunk_head *head;
98 static mode_t
99 apply_umask(mode_t mode)
101 mode_t um;
103 um = umask(000);
104 umask(um);
105 return mode & ~um;
108 static const struct got_error *
109 send_patch(struct imsgbuf *ibuf, int fd)
111 const struct got_error *err = NULL;
113 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
114 NULL, 0) == -1) {
115 err = got_error_from_errno(
116 "imsg_compose GOT_IMSG_PATCH_FILE");
117 close(fd);
118 return err;
121 return got_privsep_flush_imsg(ibuf);
124 static void
125 patch_free(struct got_patch *p)
127 struct got_patch_hunk *h;
128 size_t i;
130 while (!STAILQ_EMPTY(&p->head)) {
131 h = STAILQ_FIRST(&p->head);
132 STAILQ_REMOVE_HEAD(&p->head, entries);
134 for (i = 0; i < h->len; ++i)
135 free(h->lines[i].line);
136 free(h->lines);
137 free(h);
140 free(p->new);
141 free(p->old);
143 memset(p, 0, sizeof(*p));
144 STAILQ_INIT(&p->head);
147 static const struct got_error *
148 pushline(struct got_patch_hunk *h, const char *line, size_t len)
150 void *t;
151 size_t newcap;
153 if (h->len == h->cap) {
154 if ((newcap = h->cap * 1.5) == 0)
155 newcap = 16;
156 t = recallocarray(h->lines, h->cap, newcap,
157 sizeof(h->lines[0]));
158 if (t == NULL)
159 return got_error_from_errno("recallocarray");
160 h->lines = t;
161 h->cap = newcap;
164 if ((t = malloc(len - 1)) == NULL)
165 return got_error_from_errno("malloc");
166 memcpy(t, line + 1, len - 1); /* skip the line type */
168 h->lines[h->len].mode = *line;
169 h->lines[h->len].line = t;
170 h->lines[h->len].len = len - 2; /* line type and trailing NUL */
171 h->len++;
172 return NULL;
175 static const struct got_error *
176 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
178 const struct got_error *err = NULL;
179 struct imsg imsg;
180 struct got_imsg_patch_hunk hdr;
181 struct got_imsg_patch patch;
182 struct got_patch_hunk *h = NULL;
183 size_t datalen;
184 int lastmode = -1;
186 memset(p, 0, sizeof(*p));
187 STAILQ_INIT(&p->head);
189 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
190 if (err)
191 return err;
192 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
193 *done = 1;
194 goto done;
196 if (imsg.hdr.type != GOT_IMSG_PATCH) {
197 err = got_error(GOT_ERR_PRIVSEP_MSG);
198 goto done;
200 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
201 if (datalen != sizeof(patch)) {
202 err = got_error(GOT_ERR_PRIVSEP_LEN);
203 goto done;
205 memcpy(&patch, imsg.data, sizeof(patch));
207 if (patch.old[sizeof(patch.old)-1] != '\0' ||
208 patch.new[sizeof(patch.new)-1] != '\0' ||
209 patch.cid[sizeof(patch.cid)-1] != '\0' ||
210 patch.blob[sizeof(patch.blob)-1] != '\0') {
211 err = got_error(GOT_ERR_PRIVSEP_LEN);
212 goto done;
215 if (*patch.cid != '\0')
216 strlcpy(p->cid, patch.cid, sizeof(p->cid));
218 if (*patch.blob != '\0')
219 strlcpy(p->blob, patch.blob, sizeof(p->blob));
221 p->xbit = patch.xbit;
223 /* automatically set strip=1 for git-style diffs */
224 if (strip == -1 && patch.git &&
225 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
226 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
227 strip = 1;
229 /* prefer the new name if not /dev/null for not git-style diffs */
230 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
231 err = got_path_strip(&p->old, patch.new, strip);
232 if (err)
233 goto done;
234 } else if (*patch.old != '\0') {
235 err = got_path_strip(&p->old, patch.old, strip);
236 if (err)
237 goto done;
240 if (*patch.new != '\0') {
241 err = got_path_strip(&p->new, patch.new, strip);
242 if (err)
243 goto done;
246 if (p->old == NULL && p->new == NULL) {
247 err = got_error(GOT_ERR_PATCH_MALFORMED);
248 goto done;
251 imsg_free(&imsg);
253 for (;;) {
254 char *t;
256 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
257 if (err) {
258 patch_free(p);
259 return err;
262 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
263 switch (imsg.hdr.type) {
264 case GOT_IMSG_PATCH_DONE:
265 if (h != NULL && h->len == 0)
266 err = got_error(GOT_ERR_PATCH_MALFORMED);
267 goto done;
268 case GOT_IMSG_PATCH_HUNK:
269 if (h != NULL &&
270 (h->len == 0 || h->old_nonl || h->new_nonl)) {
271 err = got_error(GOT_ERR_PATCH_MALFORMED);
272 goto done;
274 lastmode = -1;
275 if (datalen != sizeof(hdr)) {
276 err = got_error(GOT_ERR_PRIVSEP_LEN);
277 goto done;
279 memcpy(&hdr, imsg.data, sizeof(hdr));
280 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
281 err = got_error(GOT_ERR_PRIVSEP_LEN);
282 goto done;
284 if ((h = calloc(1, sizeof(*h))) == NULL) {
285 err = got_error_from_errno("calloc");
286 goto done;
288 h->old_from = hdr.oldfrom;
289 h->old_lines = hdr.oldlines;
290 h->new_from = hdr.newfrom;
291 h->new_lines = hdr.newlines;
292 STAILQ_INSERT_TAIL(&p->head, h, entries);
293 break;
294 case GOT_IMSG_PATCH_LINE:
295 if (h == NULL) {
296 err = got_error(GOT_ERR_PRIVSEP_MSG);
297 goto done;
299 t = imsg.data;
300 /* at least one char */
301 if (datalen < 2 || t[datalen-1] != '\0') {
302 err = got_error(GOT_ERR_PRIVSEP_MSG);
303 goto done;
305 if (*t != ' ' && *t != '-' && *t != '+' &&
306 *t != '\\') {
307 err = got_error(GOT_ERR_PRIVSEP_MSG);
308 goto done;
311 if (*t != '\\')
312 err = pushline(h, t, datalen);
313 else if (lastmode == '-')
314 h->old_nonl = 1;
315 else if (lastmode == '+')
316 h->new_nonl = 1;
317 else
318 err = got_error(GOT_ERR_PATCH_MALFORMED);
320 if (err)
321 goto done;
323 lastmode = *t;
324 break;
325 default:
326 err = got_error(GOT_ERR_PRIVSEP_MSG);
327 goto done;
330 imsg_free(&imsg);
333 done:
334 if (err)
335 patch_free(p);
337 imsg_free(&imsg);
338 return err;
341 static void
342 reverse_patch(struct got_patch *p)
344 struct got_patch_hunk *h;
345 size_t i;
346 int tmp;
348 STAILQ_FOREACH(h, &p->head, entries) {
349 tmp = h->old_from;
350 h->old_from = h->new_from;
351 h->new_from = tmp;
353 tmp = h->old_lines;
354 h->old_lines = h->new_lines;
355 h->new_lines = tmp;
357 tmp = h->old_nonl;
358 h->old_nonl = h->new_nonl;
359 h->new_nonl = tmp;
361 for (i = 0; i < h->len; ++i) {
362 if (h->lines[i].mode == '+')
363 h->lines[i].mode = '-';
364 else if (h->lines[i].mode == '-')
365 h->lines[i].mode = '+';
371 * Copy data from orig starting at copypos until pos into tmp.
372 * If pos is -1, copy until EOF.
374 static const struct got_error *
375 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
377 char buf[BUFSIZ];
378 size_t len, r, w;
380 if (fseeko(orig, copypos, SEEK_SET) == -1)
381 return got_error_from_errno("fseeko");
383 while (pos == -1 || copypos < pos) {
384 len = sizeof(buf);
385 if (pos > 0)
386 len = MIN(len, (size_t)pos - copypos);
387 r = fread(buf, 1, len, orig);
388 if (r != len && ferror(orig))
389 return got_error_from_errno("fread");
390 w = fwrite(buf, 1, r, tmp);
391 if (w != r)
392 return got_error_from_errno("fwrite");
393 copypos += len;
394 if (r != len && feof(orig)) {
395 if (pos == -1)
396 return NULL;
397 return got_error(GOT_ERR_HUNK_FAILED);
400 return NULL;
403 static int lines_eq(struct got_patch_line *, const char *, size_t, int *);
405 static const struct got_error *
406 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
408 const struct got_error *err = NULL;
409 struct got_patch_line *l = &h->lines[0];
410 char *line = NULL;
411 char mode = l->mode;
412 size_t linesize = 0;
413 ssize_t linelen;
414 off_t match = -1;
415 int mangled = 0, match_lineno = -1;
417 for (;;) {
418 (*lineno)++;
419 linelen = getline(&line, &linesize, orig);
420 if (linelen == -1) {
421 if (ferror(orig))
422 err = got_error_from_errno("getline");
423 /* An EOF is fine iff the target file is empty. */
424 if (feof(orig) && match == -1 && h->old_lines != 0)
425 err = got_error(GOT_ERR_HUNK_FAILED);
426 match = 0;
427 match_lineno = (*lineno)-1;
428 break;
431 if ((mode == ' ' && lines_eq(l, line, linelen, &mangled)) ||
432 (mode == '-' && lines_eq(l, line, linelen, &mangled)) ||
433 (mode == '+' && *lineno == h->old_from)) {
434 match = ftello(orig);
435 if (match == -1) {
436 err = got_error_from_errno("ftello");
437 break;
439 match -= linelen;
440 match_lineno = (*lineno)-1;
443 if (*lineno >= h->old_from && match != -1) {
444 if (mangled)
445 h->ws_mangled = 1;
446 break;
450 if (err == NULL) {
451 *pos = match;
452 *lineno = match_lineno;
453 if (fseeko(orig, match, SEEK_SET) == -1)
454 err = got_error_from_errno("fseeko");
457 free(line);
458 return err;
461 static int
462 lines_eq(struct got_patch_line *l, const char *b, size_t len, int *mangled)
464 char *a = l->line;
465 size_t i, j;
467 if (len > 00 && b[len - 1] == '\n')
468 len--;
470 *mangled = 0;
471 if (l->len == len && !memcmp(a, b, len))
472 return 1;
474 *mangled = 1;
476 i = j = 0;
477 for (;;) {
478 while (i < l->len &&
479 (a[i] == '\t' || a[i] == ' ' || a[i] == '\f'))
480 i++;
481 while (j < len &&
482 (b[j] == '\t' || b[j] == ' ' || b[j] == '\f'))
483 j++;
484 if (i == l->len || j == len || a[i] != b[j])
485 break;
486 i++, j++;
489 return (i == l->len && j == len);
492 static const struct got_error *
493 test_hunk(FILE *orig, struct got_patch_hunk *h)
495 const struct got_error *err = NULL;
496 char *line = NULL;
497 size_t linesize = 0, i = 0;
498 ssize_t linelen;
499 int mangled;
501 for (i = 0; i < h->len; ++i) {
502 switch (h->lines[i].mode) {
503 case '+':
504 continue;
505 case ' ':
506 case '-':
507 linelen = getline(&line, &linesize, orig);
508 if (linelen == -1) {
509 if (ferror(orig))
510 err = got_error_from_errno("getline");
511 else
512 err = got_error(
513 GOT_ERR_HUNK_FAILED);
514 goto done;
516 if (!lines_eq(&h->lines[i], line, linelen, &mangled)) {
517 err = got_error(GOT_ERR_HUNK_FAILED);
518 goto done;
520 if (mangled)
521 h->ws_mangled = 1;
522 break;
526 done:
527 free(line);
528 return err;
531 static const struct got_error *
532 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
533 off_t from)
535 const struct got_error *err = NULL;
536 const char *t;
537 size_t linesize = 0, i, new = 0;
538 char *line = NULL;
539 char mode;
540 size_t l;
541 ssize_t linelen;
543 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
544 return got_error_from_errno("fseeko");
546 for (i = 0; i < h->len; ++i) {
547 switch (mode = h->lines[i].mode) {
548 case '-':
549 case ' ':
550 (*lineno)++;
551 if (orig != NULL) {
552 linelen = getline(&line, &linesize, orig);
553 if (linelen == -1) {
554 err = got_error_from_errno("getline");
555 goto done;
557 if (line[linelen - 1] == '\n')
558 line[linelen - 1] = '\0';
559 t = line;
560 l = linelen - 1;
561 } else {
562 t = h->lines[i].line;
563 l = h->lines[i].len;
565 if (mode == '-')
566 continue;
567 if (fwrite(t, 1, l, tmp) != l ||
568 fputc('\n', tmp) == EOF) {
569 err = got_error_from_errno("fprintf");
570 goto done;
572 break;
573 case '+':
574 new++;
575 t = h->lines[i].line;
576 l = h->lines[i].len;
577 if (fwrite(t, 1, l, tmp) != l) {
578 err = got_error_from_errno("fprintf");
579 goto done;
581 if (new != h->new_lines || !h->new_nonl) {
582 if (fprintf(tmp, "\n") < 0) {
583 err = got_error_from_errno("fprintf");
584 goto done;
587 break;
591 done:
592 free(line);
593 return err;
596 static const struct got_error *
597 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
599 const struct got_error *err = NULL;
600 struct got_patch_hunk *h;
601 struct stat sb;
602 int lineno = 0;
603 off_t copypos, pos;
604 char *line = NULL;
605 size_t linesize = 0;
606 ssize_t linelen;
608 if (p->old == NULL) { /* create */
609 h = STAILQ_FIRST(&p->head);
610 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
611 return got_error(GOT_ERR_PATCH_MALFORMED);
612 return apply_hunk(orig, tmp, h, &lineno, 0);
615 /* When deleting binary files there are no hunks to apply. */
616 if (p->new == NULL && STAILQ_EMPTY(&p->head))
617 return NULL;
619 if (fstat(fileno(orig), &sb) == -1)
620 return got_error_from_errno("fstat");
622 copypos = 0;
623 STAILQ_FOREACH(h, &p->head, entries) {
624 tryagain:
625 err = locate_hunk(orig, h, &pos, &lineno);
626 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
627 h->err = err;
628 if (err != NULL)
629 return err;
630 err = copy(tmp, orig, copypos, pos);
631 if (err != NULL)
632 return err;
633 copypos = pos;
635 err = test_hunk(orig, h);
636 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
638 * try to apply the hunk again starting the search
639 * after the previous partial match.
641 if (fseeko(orig, pos, SEEK_SET) == -1)
642 return got_error_from_errno("fseeko");
643 linelen = getline(&line, &linesize, orig);
644 if (linelen == -1)
645 return got_error_from_errno("getline");
646 lineno++;
647 goto tryagain;
649 if (err != NULL)
650 return err;
652 if (lineno + 1 != h->old_from)
653 h->offset = lineno + 1 - h->old_from;
655 err = apply_hunk(orig, tmp, h, &lineno, pos);
656 if (err != NULL)
657 return err;
659 copypos = ftello(orig);
660 if (copypos == -1)
661 return got_error_from_errno("ftello");
664 if (p->new == NULL && sb.st_size != copypos) {
665 h = STAILQ_FIRST(&p->head);
666 h->err = got_error(GOT_ERR_HUNK_FAILED);
667 err = h->err;
668 } else if (!feof(orig))
669 err = copy(tmp, orig, copypos, -1);
671 return err;
674 static const struct got_error *
675 report_progress(struct patch_args *pa, const char *old, const char *new,
676 unsigned char status, const struct got_error *orig_error)
678 const struct got_error *err;
679 struct got_patch_hunk *h;
681 err = pa->progress_cb(pa->progress_arg, old, new, status,
682 orig_error, 0, 0, 0, 0, 0, 0, NULL);
683 if (err)
684 return err;
686 STAILQ_FOREACH(h, pa->head, entries) {
687 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
688 continue;
690 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
691 h->old_from, h->old_lines, h->new_from, h->new_lines,
692 h->offset, h->ws_mangled, h->err);
693 if (err)
694 return err;
697 return NULL;
700 static const struct got_error *
701 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
702 const char *path)
704 return report_progress(arg, path, NULL, status, NULL);
707 static const struct got_error *
708 patch_add(void *arg, unsigned char status, const char *path)
710 return report_progress(arg, NULL, path, status, NULL);
713 static const struct got_error *
714 open_blob(char **path, FILE **fp, const char *blobid,
715 struct got_repository *repo)
717 const struct got_error *err = NULL;
718 struct got_blob_object *blob = NULL;
719 struct got_object_id id, *idptr, *matched_id = NULL;
720 enum got_hash_algorithm algo;
721 int fd = -1;
723 *fp = NULL;
724 *path = NULL;
725 algo = got_repo_get_object_format(repo);
727 if (strlen(blobid) != got_hash_digest_string_length(algo) - 1) {
728 err = got_repo_match_object_id(&matched_id, NULL, blobid,
729 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
730 repo);
731 if (err)
732 return err;
733 idptr = matched_id;
734 } else {
735 if (!got_parse_object_id(&id, blobid, algo))
736 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
737 idptr = &id;
740 fd = got_opentempfd();
741 if (fd == -1) {
742 err = got_error_from_errno("got_opentempfd");
743 goto done;
746 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
747 if (err)
748 goto done;
750 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
751 "");
752 if (err)
753 goto done;
755 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
756 if (err)
757 goto done;
759 done:
760 if (fd != -1 && close(fd) == -1 && err == NULL)
761 err = got_error_from_errno("close");
762 if (blob)
763 got_object_blob_close(blob);
764 if (matched_id != NULL)
765 free(matched_id);
766 if (err) {
767 if (*fp != NULL)
768 fclose(*fp);
769 if (*path != NULL)
770 unlink(*path);
771 free(*path);
772 *fp = NULL;
773 *path = NULL;
775 return err;
778 static const struct got_error *
779 prepare_merge(int *do_merge, char **apath, FILE **afile,
780 struct got_worktree *worktree, struct got_repository *repo,
781 struct got_patch *p, struct got_object_id *commit_id,
782 struct got_tree_object *tree, const char *path)
784 const struct got_error *err = NULL;
786 *do_merge = 0;
787 *apath = NULL;
788 *afile = NULL;
790 /* don't run the diff3 merge on creations/deletions */
791 if (p->old == NULL || p->new == NULL)
792 return NULL;
794 if (commit_id) {
795 struct got_object_id *id;
797 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
798 if (err)
799 return err;
800 got_object_id_hex(id, p->blob, sizeof(p->blob));
801 got_object_id_hex(commit_id, p->cid, sizeof(p->cid));
802 free(id);
803 err = open_blob(apath, afile, p->blob, repo);
804 *do_merge = err == NULL;
805 } else if (*p->blob != '\0') {
806 err = open_blob(apath, afile, p->blob, repo);
808 * ignore failures to open this blob, we might have
809 * parsed gibberish.
811 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
812 err->code != GOT_ERR_NO_OBJ)
813 return err;
814 *do_merge = err == NULL;
815 err = NULL;
818 return err;
821 static const struct got_error *
822 apply_patch(int *overlapcnt, struct got_worktree *worktree,
823 struct got_repository *repo, struct got_fileindex *fileindex,
824 const char *old, const char *new, struct got_patch *p, int nop,
825 int reverse, struct got_object_id *commit_id,
826 struct got_tree_object *tree, struct patch_args *pa,
827 got_cancel_cb cancel_cb, void *cancel_arg)
829 const struct got_error *err = NULL;
830 struct stat sb;
831 int do_merge = 0, file_renamed = 0;
832 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
833 char *oldpath = NULL, *newpath = NULL;
834 char *tmppath = NULL, *template = NULL;
835 char *apath = NULL, *mergepath = NULL;
836 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
837 int outfd;
838 mode_t mode = GOT_DEFAULT_FILE_MODE;
840 *overlapcnt = 0;
842 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
843 commit_id, tree, old);
844 if (err)
845 return err;
847 if (reverse && !do_merge)
848 reverse_patch(p);
850 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
851 old) == -1) {
852 err = got_error_from_errno("asprintf");
853 goto done;
856 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
857 new) == -1) {
858 err = got_error_from_errno("asprintf");
859 goto done;
862 file_renamed = strcmp(oldpath, newpath);
864 if (asprintf(&template, "%s/got-patch",
865 got_worktree_get_root_path(worktree)) == -1) {
866 err = got_error_from_errno(template);
867 goto done;
870 if (p->old != NULL) {
871 if ((oldfile = fopen(oldpath, "r")) == NULL) {
872 err = got_error_from_errno2("open", oldpath);
873 goto done;
875 if (fstat(fileno(oldfile), &sb) == -1) {
876 err = got_error_from_errno2("fstat", oldpath);
877 goto done;
879 mode = sb.st_mode;
880 } else if (p->xbit)
881 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
883 err = got_opentemp_named(&tmppath, &tmpfile, template, "");
884 if (err)
885 goto done;
886 outfd = fileno(tmpfile);
887 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
888 if (err)
889 goto done;
891 if (do_merge) {
892 const char *type, *id;
894 if (fseeko(afile, 0, SEEK_SET) == -1 ||
895 fseeko(oldfile, 0, SEEK_SET) == -1 ||
896 fseeko(tmpfile, 0, SEEK_SET) == -1) {
897 err = got_error_from_errno("fseeko");
898 goto done;
901 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
902 err = got_error_from_errno("asprintf");
903 oldlabel = NULL;
904 goto done;
907 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
908 err = got_error_from_errno("asprintf");
909 newlabel = NULL;
910 goto done;
913 if (*p->cid != '\0') {
914 type = "commit";
915 id = p->cid;
916 } else {
917 type = "blob";
918 id = p->blob;
921 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
922 err = got_error_from_errno("asprintf");
923 anclabel = NULL;
924 goto done;
927 if (reverse) {
928 char *s;
929 FILE *t;
931 s = anclabel;
932 anclabel = newlabel;
933 newlabel = s;
935 t = afile;
936 afile = tmpfile;
937 tmpfile = t;
940 err = got_opentemp_named(&mergepath, &mergefile, template, "");
941 if (err)
942 goto done;
943 outfd = fileno(mergefile);
945 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
946 oldfile, oldlabel, anclabel, newlabel,
947 GOT_DIFF_ALGORITHM_PATIENCE);
948 if (err)
949 goto done;
952 if (nop)
953 goto done;
955 if (p->old != NULL && p->new == NULL) {
956 err = got_worktree_patch_schedule_rm(old, repo, worktree,
957 fileindex, patch_delete, pa);
958 goto done;
961 if (fchmod(outfd, apply_umask(mode)) == -1) {
962 err = got_error_from_errno2("chmod", tmppath);
963 goto done;
966 if (mergepath) {
967 err = got_path_move_file(mergepath, newpath);
968 if (err)
969 goto done;
970 free(mergepath);
971 mergepath = NULL;
972 } else {
973 err = got_path_move_file(tmppath, newpath);
974 if (err)
975 goto done;
976 free(tmppath);
977 tmppath = NULL;
980 if (file_renamed) {
981 err = got_worktree_patch_schedule_rm(old, repo, worktree,
982 fileindex, patch_delete, pa);
983 if (err == NULL)
984 err = got_worktree_patch_schedule_add(new, repo,
985 worktree, fileindex, patch_add,
986 pa);
987 if (err)
988 unlink(newpath);
989 } else if (p->old == NULL) {
990 err = got_worktree_patch_schedule_add(new, repo, worktree,
991 fileindex, patch_add, pa);
992 if (err)
993 unlink(newpath);
994 } else if (*overlapcnt != 0)
995 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
996 else if (do_merge)
997 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
998 else
999 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
1001 done:
1002 free(template);
1004 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1005 err = got_error_from_errno("unlink");
1006 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
1007 err = got_error_from_errno("fclose");
1008 free(tmppath);
1010 free(oldpath);
1011 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
1012 err = got_error_from_errno("fclose");
1014 if (apath != NULL && unlink(apath) == -1 && err == NULL)
1015 err = got_error_from_errno("unlink");
1016 if (afile != NULL && fclose(afile) == EOF && err == NULL)
1017 err = got_error_from_errno("fclose");
1018 free(apath);
1020 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
1021 err = got_error_from_errno("unlink");
1022 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
1023 err = got_error_from_errno("fclose");
1024 free(mergepath);
1026 free(newpath);
1027 free(oldlabel);
1028 free(newlabel);
1029 free(anclabel);
1030 return err;
1033 const struct got_error *
1034 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1035 int nop, int strip, int reverse, struct got_object_id *commit_id,
1036 got_patch_progress_cb progress_cb, void *progress_arg,
1037 got_cancel_cb cancel_cb, void *cancel_arg)
1039 const struct got_error *err = NULL, *complete_err = NULL;
1040 struct got_fileindex *fileindex = NULL;
1041 struct got_commit_object *commit = NULL;
1042 struct got_tree_object *tree = NULL;
1043 char *fileindex_path = NULL;
1044 char *oldpath, *newpath;
1045 struct imsgbuf *ibuf;
1046 int imsg_fds[2] = {-1, -1};
1047 int overlapcnt, done = 0, failed = 0;
1048 pid_t pid;
1050 ibuf = calloc(1, sizeof(*ibuf));
1051 if (ibuf == NULL) {
1052 err = got_error_from_errno("calloc");
1053 goto done;
1056 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1057 err = got_error_from_errno("socketpair");
1058 goto done;
1061 pid = fork();
1062 if (pid == -1) {
1063 err = got_error_from_errno("fork");
1064 goto done;
1065 } else if (pid == 0) {
1066 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1067 NULL);
1068 /* not reached */
1071 if (close(imsg_fds[1]) == -1) {
1072 err = got_error_from_errno("close");
1073 goto done;
1075 imsg_fds[1] = -1;
1076 if (imsgbuf_init(ibuf, imsg_fds[0]) == -1) {
1077 err = got_error_from_errno("imsgbuf_init");
1078 goto done;
1080 imsgbuf_allow_fdpass(ibuf);
1082 err = send_patch(ibuf, fd);
1083 fd = -1;
1084 if (err)
1085 goto done;
1087 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1088 worktree, repo);
1089 if (err)
1090 goto done;
1092 if (commit_id) {
1093 err = got_object_open_as_commit(&commit, repo, commit_id);
1094 if (err)
1095 goto done;
1097 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1098 if (err)
1099 goto done;
1102 while (!done && err == NULL) {
1103 struct got_patch p;
1104 struct patch_args pa;
1106 pa.progress_cb = progress_cb;
1107 pa.progress_arg = progress_arg;
1108 pa.head = &p.head;
1110 err = recv_patch(ibuf, &done, &p, strip);
1111 if (err || done)
1112 break;
1114 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1115 &newpath, worktree, repo, fileindex);
1116 if (err == NULL)
1117 err = apply_patch(&overlapcnt, worktree, repo,
1118 fileindex, oldpath, newpath, &p, nop, reverse,
1119 commit_id, tree, &pa, cancel_cb, cancel_arg);
1120 if (err != NULL) {
1121 failed = 1;
1122 /* recoverable errors */
1123 if (err->code == GOT_ERR_FILE_STATUS ||
1124 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1125 err = report_progress(&pa, p.old, p.new,
1126 GOT_STATUS_CANNOT_UPDATE, err);
1127 else if (err->code == GOT_ERR_HUNK_FAILED)
1128 err = report_progress(&pa, p.old, p.new,
1129 GOT_STATUS_CANNOT_UPDATE, NULL);
1131 if (overlapcnt != 0)
1132 failed = 1;
1134 free(oldpath);
1135 free(newpath);
1136 patch_free(&p);
1138 if (err)
1139 break;
1142 done:
1143 complete_err = got_worktree_patch_complete(worktree, fileindex,
1144 fileindex_path);
1145 if (complete_err && err == NULL)
1146 err = complete_err;
1147 free(fileindex_path);
1148 if (tree)
1149 got_object_tree_close(tree);
1150 if (commit)
1151 got_object_commit_close(commit);
1152 if (fd != -1 && close(fd) == -1 && err == NULL)
1153 err = got_error_from_errno("close");
1154 if (ibuf != NULL)
1155 imsgbuf_clear(ibuf);
1156 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1157 err = got_error_from_errno("close");
1158 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1159 err = got_error_from_errno("close");
1160 if (err == NULL && failed)
1161 err = got_error(GOT_ERR_PATCH_FAILED);
1162 return err;