Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 *
16 * Apply patches.
17 *
18 * Things that we may want to support:
19 * + support indented patches?
20 * + support other kinds of patches?
21 */
23 #include <sys/types.h>
24 #include <sys/queue.h>
25 #include <sys/socket.h>
26 #include <sys/stat.h>
27 #include <sys/uio.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <sha1.h>
33 #include <sha2.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <imsg.h>
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_path.h"
44 #include "got_reference.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
47 #include "got_repository.h"
48 #include "got_opentemp.h"
49 #include "got_patch.h"
50 #include "got_diff.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_diff.h"
54 #include "got_lib_object.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_hash.h"
58 #ifndef MIN
59 #define MIN(a, b) ((a) < (b) ? (a) : (b))
60 #endif
62 struct got_patch_line {
63 char mode;
64 char *line;
65 size_t len;
66 };
68 struct got_patch_hunk {
69 STAILQ_ENTRY(got_patch_hunk) entries;
70 const struct got_error *err;
71 int ws_mangled;
72 int offset;
73 int old_nonl;
74 int new_nonl;
75 int old_from;
76 int old_lines;
77 int new_from;
78 int new_lines;
79 size_t len;
80 size_t cap;
81 struct got_patch_line *lines;
82 };
84 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
85 struct got_patch {
86 int xbit;
87 char *old;
88 char *new;
89 char cid[41];
90 char blob[41];
91 struct got_patch_hunk_head head;
92 };
94 struct patch_args {
95 got_patch_progress_cb progress_cb;
96 void *progress_arg;
97 struct got_patch_hunk_head *head;
98 };
100 static mode_t
101 apply_umask(mode_t mode)
103 mode_t um;
105 um = umask(000);
106 umask(um);
107 return mode & ~um;
110 static const struct got_error *
111 send_patch(struct imsgbuf *ibuf, int fd)
113 const struct got_error *err = NULL;
115 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
116 NULL, 0) == -1) {
117 err = got_error_from_errno(
118 "imsg_compose GOT_IMSG_PATCH_FILE");
119 close(fd);
120 return err;
123 return got_privsep_flush_imsg(ibuf);
126 static void
127 patch_free(struct got_patch *p)
129 struct got_patch_hunk *h;
130 size_t i;
132 while (!STAILQ_EMPTY(&p->head)) {
133 h = STAILQ_FIRST(&p->head);
134 STAILQ_REMOVE_HEAD(&p->head, entries);
136 for (i = 0; i < h->len; ++i)
137 free(h->lines[i].line);
138 free(h->lines);
139 free(h);
142 free(p->new);
143 free(p->old);
145 memset(p, 0, sizeof(*p));
146 STAILQ_INIT(&p->head);
149 static const struct got_error *
150 pushline(struct got_patch_hunk *h, const char *line, size_t len)
152 void *t;
153 size_t newcap;
155 if (h->len == h->cap) {
156 if ((newcap = h->cap * 1.5) == 0)
157 newcap = 16;
158 t = recallocarray(h->lines, h->cap, newcap,
159 sizeof(h->lines[0]));
160 if (t == NULL)
161 return got_error_from_errno("recallocarray");
162 h->lines = t;
163 h->cap = newcap;
166 if ((t = malloc(len - 1)) == NULL)
167 return got_error_from_errno("malloc");
168 memcpy(t, line + 1, len - 1); /* skip the line type */
170 h->lines[h->len].mode = *line;
171 h->lines[h->len].line = t;
172 h->lines[h->len].len = len - 2; /* line type and trailing NUL */
173 h->len++;
174 return NULL;
177 static const struct got_error *
178 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
180 const struct got_error *err = NULL;
181 struct imsg imsg;
182 struct got_imsg_patch_hunk hdr;
183 struct got_imsg_patch patch;
184 struct got_patch_hunk *h = NULL;
185 size_t datalen;
186 int lastmode = -1;
188 memset(p, 0, sizeof(*p));
189 STAILQ_INIT(&p->head);
191 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
192 if (err)
193 return err;
194 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
195 *done = 1;
196 goto done;
198 if (imsg.hdr.type != GOT_IMSG_PATCH) {
199 err = got_error(GOT_ERR_PRIVSEP_MSG);
200 goto done;
202 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
203 if (datalen != sizeof(patch)) {
204 err = got_error(GOT_ERR_PRIVSEP_LEN);
205 goto done;
207 memcpy(&patch, imsg.data, sizeof(patch));
209 if (patch.old[sizeof(patch.old)-1] != '\0' ||
210 patch.new[sizeof(patch.new)-1] != '\0' ||
211 patch.cid[sizeof(patch.cid)-1] != '\0' ||
212 patch.blob[sizeof(patch.blob)-1] != '\0') {
213 err = got_error(GOT_ERR_PRIVSEP_LEN);
214 goto done;
217 if (*patch.cid != '\0')
218 strlcpy(p->cid, patch.cid, sizeof(p->cid));
220 if (*patch.blob != '\0')
221 strlcpy(p->blob, patch.blob, sizeof(p->blob));
223 p->xbit = patch.xbit;
225 /* automatically set strip=1 for git-style diffs */
226 if (strip == -1 && patch.git &&
227 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
228 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
229 strip = 1;
231 /* prefer the new name if not /dev/null for not git-style diffs */
232 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
233 err = got_path_strip(&p->old, patch.new, strip);
234 if (err)
235 goto done;
236 } else if (*patch.old != '\0') {
237 err = got_path_strip(&p->old, patch.old, strip);
238 if (err)
239 goto done;
242 if (*patch.new != '\0') {
243 err = got_path_strip(&p->new, patch.new, strip);
244 if (err)
245 goto done;
248 if (p->old == NULL && p->new == NULL) {
249 err = got_error(GOT_ERR_PATCH_MALFORMED);
250 goto done;
253 imsg_free(&imsg);
255 for (;;) {
256 char *t;
258 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
259 if (err) {
260 patch_free(p);
261 return err;
264 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
265 switch (imsg.hdr.type) {
266 case GOT_IMSG_PATCH_DONE:
267 if (h != NULL && h->len == 0)
268 err = got_error(GOT_ERR_PATCH_MALFORMED);
269 goto done;
270 case GOT_IMSG_PATCH_HUNK:
271 if (h != NULL &&
272 (h->len == 0 || h->old_nonl || h->new_nonl)) {
273 err = got_error(GOT_ERR_PATCH_MALFORMED);
274 goto done;
276 lastmode = -1;
277 if (datalen != sizeof(hdr)) {
278 err = got_error(GOT_ERR_PRIVSEP_LEN);
279 goto done;
281 memcpy(&hdr, imsg.data, sizeof(hdr));
282 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
283 err = got_error(GOT_ERR_PRIVSEP_LEN);
284 goto done;
286 if ((h = calloc(1, sizeof(*h))) == NULL) {
287 err = got_error_from_errno("calloc");
288 goto done;
290 h->old_from = hdr.oldfrom;
291 h->old_lines = hdr.oldlines;
292 h->new_from = hdr.newfrom;
293 h->new_lines = hdr.newlines;
294 STAILQ_INSERT_TAIL(&p->head, h, entries);
295 break;
296 case GOT_IMSG_PATCH_LINE:
297 if (h == NULL) {
298 err = got_error(GOT_ERR_PRIVSEP_MSG);
299 goto done;
301 t = imsg.data;
302 /* at least one char */
303 if (datalen < 2 || t[datalen-1] != '\0') {
304 err = got_error(GOT_ERR_PRIVSEP_MSG);
305 goto done;
307 if (*t != ' ' && *t != '-' && *t != '+' &&
308 *t != '\\') {
309 err = got_error(GOT_ERR_PRIVSEP_MSG);
310 goto done;
313 if (*t != '\\')
314 err = pushline(h, t, datalen);
315 else if (lastmode == '-')
316 h->old_nonl = 1;
317 else if (lastmode == '+')
318 h->new_nonl = 1;
319 else
320 err = got_error(GOT_ERR_PATCH_MALFORMED);
322 if (err)
323 goto done;
325 lastmode = *t;
326 break;
327 default:
328 err = got_error(GOT_ERR_PRIVSEP_MSG);
329 goto done;
332 imsg_free(&imsg);
335 done:
336 if (err)
337 patch_free(p);
339 imsg_free(&imsg);
340 return err;
343 static void
344 reverse_patch(struct got_patch *p)
346 struct got_patch_hunk *h;
347 size_t i;
348 int tmp;
350 STAILQ_FOREACH(h, &p->head, entries) {
351 tmp = h->old_from;
352 h->old_from = h->new_from;
353 h->new_from = tmp;
355 tmp = h->old_lines;
356 h->old_lines = h->new_lines;
357 h->new_lines = tmp;
359 tmp = h->old_nonl;
360 h->old_nonl = h->new_nonl;
361 h->new_nonl = tmp;
363 for (i = 0; i < h->len; ++i) {
364 if (h->lines[i].mode == '+')
365 h->lines[i].mode = '-';
366 else if (h->lines[i].mode == '-')
367 h->lines[i].mode = '+';
372 /*
373 * Copy data from orig starting at copypos until pos into tmp.
374 * If pos is -1, copy until EOF.
375 */
376 static const struct got_error *
377 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
379 char buf[BUFSIZ];
380 size_t len, r, w;
382 if (fseeko(orig, copypos, SEEK_SET) == -1)
383 return got_error_from_errno("fseeko");
385 while (pos == -1 || copypos < pos) {
386 len = sizeof(buf);
387 if (pos > 0)
388 len = MIN(len, (size_t)pos - copypos);
389 r = fread(buf, 1, len, orig);
390 if (r != len && ferror(orig))
391 return got_error_from_errno("fread");
392 w = fwrite(buf, 1, r, tmp);
393 if (w != r)
394 return got_error_from_errno("fwrite");
395 copypos += len;
396 if (r != len && feof(orig)) {
397 if (pos == -1)
398 return NULL;
399 return got_error(GOT_ERR_HUNK_FAILED);
402 return NULL;
405 static int lines_eq(struct got_patch_line *, const char *, size_t, int *);
407 static const struct got_error *
408 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
410 const struct got_error *err = NULL;
411 struct got_patch_line *l = &h->lines[0];
412 char *line = NULL;
413 char mode = l->mode;
414 size_t linesize = 0;
415 ssize_t linelen;
416 off_t match = -1;
417 int mangled = 0, match_lineno = -1;
419 for (;;) {
420 (*lineno)++;
421 linelen = getline(&line, &linesize, orig);
422 if (linelen == -1) {
423 if (ferror(orig))
424 err = got_error_from_errno("getline");
425 /* An EOF is fine iff the target file is empty. */
426 if (feof(orig) && match == -1 && h->old_lines != 0)
427 err = got_error(GOT_ERR_HUNK_FAILED);
428 match = 0;
429 match_lineno = (*lineno)-1;
430 break;
433 if ((mode == ' ' && lines_eq(l, line, linelen, &mangled)) ||
434 (mode == '-' && lines_eq(l, line, linelen, &mangled)) ||
435 (mode == '+' && *lineno == h->old_from)) {
436 match = ftello(orig);
437 if (match == -1) {
438 err = got_error_from_errno("ftello");
439 break;
441 match -= linelen;
442 match_lineno = (*lineno)-1;
445 if (*lineno >= h->old_from && match != -1) {
446 if (mangled)
447 h->ws_mangled = 1;
448 break;
452 if (err == NULL) {
453 *pos = match;
454 *lineno = match_lineno;
455 if (fseeko(orig, match, SEEK_SET) == -1)
456 err = got_error_from_errno("fseeko");
459 free(line);
460 return err;
463 static int
464 lines_eq(struct got_patch_line *l, const char *b, size_t len, int *mangled)
466 char *a = l->line;
467 size_t i, j;
469 if (len > 00 && b[len - 1] == '\n')
470 len--;
472 *mangled = 0;
473 if (l->len == len && !memcmp(a, b, len))
474 return 1;
476 *mangled = 1;
478 i = j = 0;
479 for (;;) {
480 while (i < l->len &&
481 (a[i] == '\t' || a[i] == ' ' || a[i] == '\f'))
482 i++;
483 while (j < len &&
484 (b[j] == '\t' || b[j] == ' ' || b[j] == '\f'))
485 j++;
486 if (i == l->len || j == len || a[i] != b[j])
487 break;
488 i++, j++;
491 return (i == l->len && j == len);
494 static const struct got_error *
495 test_hunk(FILE *orig, struct got_patch_hunk *h)
497 const struct got_error *err = NULL;
498 char *line = NULL;
499 size_t linesize = 0, i = 0;
500 ssize_t linelen;
501 int mangled;
503 for (i = 0; i < h->len; ++i) {
504 switch (h->lines[i].mode) {
505 case '+':
506 continue;
507 case ' ':
508 case '-':
509 linelen = getline(&line, &linesize, orig);
510 if (linelen == -1) {
511 if (ferror(orig))
512 err = got_error_from_errno("getline");
513 else
514 err = got_error(
515 GOT_ERR_HUNK_FAILED);
516 goto done;
518 if (!lines_eq(&h->lines[i], line, linelen, &mangled)) {
519 err = got_error(GOT_ERR_HUNK_FAILED);
520 goto done;
522 if (mangled)
523 h->ws_mangled = 1;
524 break;
528 done:
529 free(line);
530 return err;
533 static const struct got_error *
534 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
535 off_t from)
537 const struct got_error *err = NULL;
538 const char *t;
539 size_t linesize = 0, i, new = 0;
540 char *line = NULL;
541 char mode;
542 size_t l;
543 ssize_t linelen;
545 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
546 return got_error_from_errno("fseeko");
548 for (i = 0; i < h->len; ++i) {
549 switch (mode = h->lines[i].mode) {
550 case '-':
551 case ' ':
552 (*lineno)++;
553 if (orig != NULL) {
554 linelen = getline(&line, &linesize, orig);
555 if (linelen == -1) {
556 err = got_error_from_errno("getline");
557 goto done;
559 if (line[linelen - 1] == '\n')
560 line[linelen - 1] = '\0';
561 t = line;
562 l = linelen - 1;
563 } else {
564 t = h->lines[i].line;
565 l = h->lines[i].len;
567 if (mode == '-')
568 continue;
569 if (fwrite(t, 1, l, tmp) != l ||
570 fputc('\n', tmp) == EOF) {
571 err = got_error_from_errno("fprintf");
572 goto done;
574 break;
575 case '+':
576 new++;
577 t = h->lines[i].line;
578 l = h->lines[i].len;
579 if (fwrite(t, 1, l, tmp) != l) {
580 err = got_error_from_errno("fprintf");
581 goto done;
583 if (new != h->new_lines || !h->new_nonl) {
584 if (fprintf(tmp, "\n") < 0) {
585 err = got_error_from_errno("fprintf");
586 goto done;
589 break;
593 done:
594 free(line);
595 return err;
598 static const struct got_error *
599 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
601 const struct got_error *err = NULL;
602 struct got_patch_hunk *h;
603 struct stat sb;
604 int lineno = 0;
605 off_t copypos, pos;
606 char *line = NULL;
607 size_t linesize = 0;
608 ssize_t linelen;
610 if (p->old == NULL) { /* create */
611 h = STAILQ_FIRST(&p->head);
612 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
613 return got_error(GOT_ERR_PATCH_MALFORMED);
614 return apply_hunk(orig, tmp, h, &lineno, 0);
617 /* When deleting binary files there are no hunks to apply. */
618 if (p->new == NULL && STAILQ_EMPTY(&p->head))
619 return NULL;
621 if (fstat(fileno(orig), &sb) == -1)
622 return got_error_from_errno("fstat");
624 copypos = 0;
625 STAILQ_FOREACH(h, &p->head, entries) {
626 tryagain:
627 err = locate_hunk(orig, h, &pos, &lineno);
628 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
629 h->err = err;
630 if (err != NULL)
631 return err;
632 err = copy(tmp, orig, copypos, pos);
633 if (err != NULL)
634 return err;
635 copypos = pos;
637 err = test_hunk(orig, h);
638 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
639 /*
640 * try to apply the hunk again starting the search
641 * after the previous partial match.
642 */
643 if (fseeko(orig, pos, SEEK_SET) == -1)
644 return got_error_from_errno("fseeko");
645 linelen = getline(&line, &linesize, orig);
646 if (linelen == -1)
647 return got_error_from_errno("getline");
648 lineno++;
649 goto tryagain;
651 if (err != NULL)
652 return err;
654 if (lineno + 1 != h->old_from)
655 h->offset = lineno + 1 - h->old_from;
657 err = apply_hunk(orig, tmp, h, &lineno, pos);
658 if (err != NULL)
659 return err;
661 copypos = ftello(orig);
662 if (copypos == -1)
663 return got_error_from_errno("ftello");
666 if (p->new == NULL && sb.st_size != copypos) {
667 h = STAILQ_FIRST(&p->head);
668 h->err = got_error(GOT_ERR_HUNK_FAILED);
669 err = h->err;
670 } else if (!feof(orig))
671 err = copy(tmp, orig, copypos, -1);
673 return err;
676 static const struct got_error *
677 report_progress(struct patch_args *pa, const char *old, const char *new,
678 unsigned char status, const struct got_error *orig_error)
680 const struct got_error *err;
681 struct got_patch_hunk *h;
683 err = pa->progress_cb(pa->progress_arg, old, new, status,
684 orig_error, 0, 0, 0, 0, 0, 0, NULL);
685 if (err)
686 return err;
688 STAILQ_FOREACH(h, pa->head, entries) {
689 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
690 continue;
692 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
693 h->old_from, h->old_lines, h->new_from, h->new_lines,
694 h->offset, h->ws_mangled, h->err);
695 if (err)
696 return err;
699 return NULL;
702 static const struct got_error *
703 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
704 const char *path)
706 return report_progress(arg, path, NULL, status, NULL);
709 static const struct got_error *
710 patch_add(void *arg, unsigned char status, const char *path)
712 return report_progress(arg, NULL, path, status, NULL);
715 static const struct got_error *
716 open_blob(char **path, FILE **fp, const char *blobid,
717 struct got_repository *repo)
719 const struct got_error *err = NULL;
720 struct got_blob_object *blob = NULL;
721 struct got_object_id id, *idptr, *matched_id = NULL;
722 int fd = -1;
724 *fp = NULL;
725 *path = NULL;
727 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 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, GOT_HASH_SHA1))
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_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
801 got_sha1_digest_to_str(commit_id->sha1, 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);
807 /*
808 * ignore failures to open this blob, we might have
809 * parsed gibberish.
810 */
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 imsg_init(ibuf, imsg_fds[0]);
1078 err = send_patch(ibuf, fd);
1079 fd = -1;
1080 if (err)
1081 goto done;
1083 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1084 worktree);
1085 if (err)
1086 goto done;
1088 if (commit_id) {
1089 err = got_object_open_as_commit(&commit, repo, commit_id);
1090 if (err)
1091 goto done;
1093 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1094 if (err)
1095 goto done;
1098 while (!done && err == NULL) {
1099 struct got_patch p;
1100 struct patch_args pa;
1102 pa.progress_cb = progress_cb;
1103 pa.progress_arg = progress_arg;
1104 pa.head = &p.head;
1106 err = recv_patch(ibuf, &done, &p, strip);
1107 if (err || done)
1108 break;
1110 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1111 &newpath, worktree, repo, fileindex);
1112 if (err == NULL)
1113 err = apply_patch(&overlapcnt, worktree, repo,
1114 fileindex, oldpath, newpath, &p, nop, reverse,
1115 commit_id, tree, &pa, cancel_cb, cancel_arg);
1116 if (err != NULL) {
1117 failed = 1;
1118 /* recoverable errors */
1119 if (err->code == GOT_ERR_FILE_STATUS ||
1120 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1121 err = report_progress(&pa, p.old, p.new,
1122 GOT_STATUS_CANNOT_UPDATE, err);
1123 else if (err->code == GOT_ERR_HUNK_FAILED)
1124 err = report_progress(&pa, p.old, p.new,
1125 GOT_STATUS_CANNOT_UPDATE, NULL);
1127 if (overlapcnt != 0)
1128 failed = 1;
1130 free(oldpath);
1131 free(newpath);
1132 patch_free(&p);
1134 if (err)
1135 break;
1138 done:
1139 if (fileindex != NULL)
1140 complete_err = got_worktree_patch_complete(fileindex,
1141 fileindex_path);
1142 if (complete_err && err == NULL)
1143 err = complete_err;
1144 free(fileindex_path);
1145 if (tree)
1146 got_object_tree_close(tree);
1147 if (commit)
1148 got_object_commit_close(commit);
1149 if (fd != -1 && close(fd) == -1 && err == NULL)
1150 err = got_error_from_errno("close");
1151 if (ibuf != NULL)
1152 imsg_clear(ibuf);
1153 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1154 err = got_error_from_errno("close");
1155 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1156 err = got_error_from_errno("close");
1157 if (err == NULL && failed)
1158 err = got_error(GOT_ERR_PATCH_FAILED);
1159 return err;