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 mode_t
90 apply_umask(mode_t mode)
91 {
92 mode_t um;
94 um = umask(000);
95 umask(um);
96 return mode & ~um;
97 }
99 static const struct got_error *
100 send_patch(struct imsgbuf *ibuf, int fd)
102 const struct got_error *err = NULL;
104 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
105 NULL, 0) == -1) {
106 err = got_error_from_errno(
107 "imsg_compose GOT_IMSG_PATCH_FILE");
108 close(fd);
109 return err;
112 return got_privsep_flush_imsg(ibuf);
115 static void
116 patch_free(struct got_patch *p)
118 struct got_patch_hunk *h;
119 size_t i;
121 while (!STAILQ_EMPTY(&p->head)) {
122 h = STAILQ_FIRST(&p->head);
123 STAILQ_REMOVE_HEAD(&p->head, entries);
125 for (i = 0; i < h->len; ++i)
126 free(h->lines[i]);
127 free(h->lines);
128 free(h);
131 free(p->new);
132 free(p->old);
134 memset(p, 0, sizeof(*p));
135 STAILQ_INIT(&p->head);
138 static const struct got_error *
139 pushline(struct got_patch_hunk *h, const char *line)
141 void *t;
142 size_t newcap;
144 if (h->len == h->cap) {
145 if ((newcap = h->cap * 1.5) == 0)
146 newcap = 16;
147 t = recallocarray(h->lines, h->cap, newcap,
148 sizeof(h->lines[0]));
149 if (t == NULL)
150 return got_error_from_errno("recallocarray");
151 h->lines = t;
152 h->cap = newcap;
155 if ((t = strdup(line)) == NULL)
156 return got_error_from_errno("strdup");
158 h->lines[h->len++] = t;
159 return NULL;
162 static const struct got_error *
163 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
165 const struct got_error *err = NULL;
166 struct imsg imsg;
167 struct got_imsg_patch_hunk hdr;
168 struct got_imsg_patch patch;
169 struct got_patch_hunk *h = NULL;
170 size_t datalen;
171 int lastmode = -1;
173 memset(p, 0, sizeof(*p));
174 STAILQ_INIT(&p->head);
176 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
177 if (err)
178 return err;
179 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
180 *done = 1;
181 goto done;
183 if (imsg.hdr.type != GOT_IMSG_PATCH) {
184 err = got_error(GOT_ERR_PRIVSEP_MSG);
185 goto done;
187 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
188 if (datalen != sizeof(patch)) {
189 err = got_error(GOT_ERR_PRIVSEP_LEN);
190 goto done;
192 memcpy(&patch, imsg.data, sizeof(patch));
194 if (patch.old[sizeof(patch.old)-1] != '\0' ||
195 patch.new[sizeof(patch.new)-1] != '\0' ||
196 patch.cid[sizeof(patch.cid)-1] != '\0' ||
197 patch.blob[sizeof(patch.blob)-1] != '\0') {
198 err = got_error(GOT_ERR_PRIVSEP_LEN);
199 goto done;
202 if (*patch.cid != '\0')
203 strlcpy(p->cid, patch.cid, sizeof(p->cid));
205 if (*patch.blob != '\0')
206 strlcpy(p->blob, patch.blob, sizeof(p->blob));
208 p->xbit = patch.xbit;
210 /* automatically set strip=1 for git-style diffs */
211 if (strip == -1 && patch.git &&
212 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
213 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
214 strip = 1;
216 /* prefer the new name if not /dev/null for not git-style diffs */
217 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
218 err = got_path_strip(&p->old, patch.new, strip);
219 if (err)
220 goto done;
221 } else if (*patch.old != '\0') {
222 err = got_path_strip(&p->old, patch.old, strip);
223 if (err)
224 goto done;
227 if (*patch.new != '\0') {
228 err = got_path_strip(&p->new, patch.new, strip);
229 if (err)
230 goto done;
233 if (p->old == NULL && p->new == NULL) {
234 err = got_error(GOT_ERR_PATCH_MALFORMED);
235 goto done;
238 imsg_free(&imsg);
240 for (;;) {
241 char *t;
243 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
244 if (err) {
245 patch_free(p);
246 return err;
249 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
250 switch (imsg.hdr.type) {
251 case GOT_IMSG_PATCH_DONE:
252 if (h != NULL && h->len == 0)
253 err = got_error(GOT_ERR_PATCH_MALFORMED);
254 goto done;
255 case GOT_IMSG_PATCH_HUNK:
256 if (h != NULL &&
257 (h->len == 0 || h->old_nonl || h->new_nonl)) {
258 err = got_error(GOT_ERR_PATCH_MALFORMED);
259 goto done;
261 lastmode = -1;
262 if (datalen != sizeof(hdr)) {
263 err = got_error(GOT_ERR_PRIVSEP_LEN);
264 goto done;
266 memcpy(&hdr, imsg.data, sizeof(hdr));
267 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
268 err = got_error(GOT_ERR_PRIVSEP_LEN);
269 goto done;
271 if ((h = calloc(1, sizeof(*h))) == NULL) {
272 err = got_error_from_errno("calloc");
273 goto done;
275 h->old_from = hdr.oldfrom;
276 h->old_lines = hdr.oldlines;
277 h->new_from = hdr.newfrom;
278 h->new_lines = hdr.newlines;
279 STAILQ_INSERT_TAIL(&p->head, h, entries);
280 break;
281 case GOT_IMSG_PATCH_LINE:
282 if (h == NULL) {
283 err = got_error(GOT_ERR_PRIVSEP_MSG);
284 goto done;
286 t = imsg.data;
287 /* at least one char */
288 if (datalen < 2 || t[datalen-1] != '\0') {
289 err = got_error(GOT_ERR_PRIVSEP_MSG);
290 goto done;
292 if (*t != ' ' && *t != '-' && *t != '+' &&
293 *t != '\\') {
294 err = got_error(GOT_ERR_PRIVSEP_MSG);
295 goto done;
298 if (*t != '\\')
299 err = pushline(h, t);
300 else if (lastmode == '-')
301 h->old_nonl = 1;
302 else if (lastmode == '+')
303 h->new_nonl = 1;
304 else
305 err = got_error(GOT_ERR_PATCH_MALFORMED);
307 if (err)
308 goto done;
310 lastmode = *t;
311 break;
312 default:
313 err = got_error(GOT_ERR_PRIVSEP_MSG);
314 goto done;
317 imsg_free(&imsg);
320 done:
321 if (err)
322 patch_free(p);
324 imsg_free(&imsg);
325 return err;
328 static void
329 reverse_patch(struct got_patch *p)
331 struct got_patch_hunk *h;
332 size_t i;
333 int tmp;
335 STAILQ_FOREACH(h, &p->head, entries) {
336 tmp = h->old_from;
337 h->old_from = h->new_from;
338 h->new_from = tmp;
340 tmp = h->old_lines;
341 h->old_lines = h->new_lines;
342 h->new_lines = tmp;
344 tmp = h->old_nonl;
345 h->old_nonl = h->new_nonl;
346 h->new_nonl = tmp;
348 for (i = 0; i < h->len; ++i) {
349 if (*h->lines[i] == '+')
350 *h->lines[i] = '-';
351 else if (*h->lines[i] == '-')
352 *h->lines[i] = '+';
357 /*
358 * Copy data from orig starting at copypos until pos into tmp.
359 * If pos is -1, copy until EOF.
360 */
361 static const struct got_error *
362 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
364 char buf[BUFSIZ];
365 size_t len, r, w;
367 if (fseeko(orig, copypos, SEEK_SET) == -1)
368 return got_error_from_errno("fseeko");
370 while (pos == -1 || copypos < pos) {
371 len = sizeof(buf);
372 if (pos > 0)
373 len = MIN(len, (size_t)pos - copypos);
374 r = fread(buf, 1, len, orig);
375 if (r != len && ferror(orig))
376 return got_error_from_errno("fread");
377 w = fwrite(buf, 1, r, tmp);
378 if (w != r)
379 return got_error_from_errno("fwrite");
380 copypos += len;
381 if (r != len && feof(orig)) {
382 if (pos == -1)
383 return NULL;
384 return got_error(GOT_ERR_HUNK_FAILED);
387 return NULL;
390 static int linecmp(const char *, const char *, int *);
392 static const struct got_error *
393 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
395 const struct got_error *err = NULL;
396 char *line = NULL;
397 char mode = *h->lines[0];
398 size_t linesize = 0;
399 ssize_t linelen;
400 off_t match = -1;
401 int mangled = 0, match_lineno = -1;
403 for (;;) {
404 linelen = getline(&line, &linesize, orig);
405 if (linelen == -1) {
406 if (ferror(orig))
407 err = got_error_from_errno("getline");
408 else if (match == -1)
409 err = got_error(GOT_ERR_HUNK_FAILED);
410 break;
412 if (line[linelen - 1] == '\n')
413 line[linelen - 1] = '\0';
414 (*lineno)++;
416 if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
417 (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
418 (mode == '+' && *lineno == h->old_from)) {
419 match = ftello(orig);
420 if (match == -1) {
421 err = got_error_from_errno("ftello");
422 break;
424 match -= linelen;
425 match_lineno = (*lineno)-1;
428 if (*lineno >= h->old_from && match != -1) {
429 if (mangled)
430 h->ws_mangled = 1;
431 break;
435 if (err == NULL) {
436 *pos = match;
437 *lineno = match_lineno;
438 if (fseeko(orig, match, SEEK_SET) == -1)
439 err = got_error_from_errno("fseeko");
442 free(line);
443 return err;
446 static int
447 linecmp(const char *a, const char *b, int *mangled)
449 int c;
451 *mangled = 0;
452 c = strcmp(a, b);
453 if (c == 0)
454 return c;
456 *mangled = 1;
457 for (;;) {
458 while (*a == '\t' || *a == ' ' || *a == '\f')
459 a++;
460 while (*b == '\t' || *b == ' ' || *b == '\f')
461 b++;
462 if (*a == '\0' || *a != *b)
463 break;
464 a++, b++;
467 return *a - *b;
470 static const struct got_error *
471 test_hunk(FILE *orig, struct got_patch_hunk *h)
473 const struct got_error *err = NULL;
474 char *line = NULL;
475 size_t linesize = 0, i = 0;
476 ssize_t linelen;
477 int mangled;
479 for (i = 0; i < h->len; ++i) {
480 switch (*h->lines[i]) {
481 case '+':
482 continue;
483 case ' ':
484 case '-':
485 linelen = getline(&line, &linesize, orig);
486 if (linelen == -1) {
487 if (ferror(orig))
488 err = got_error_from_errno("getline");
489 else
490 err = got_error(
491 GOT_ERR_HUNK_FAILED);
492 goto done;
494 if (line[linelen - 1] == '\n')
495 line[linelen - 1] = '\0';
496 if (linecmp(h->lines[i] + 1, line, &mangled)) {
497 err = got_error(GOT_ERR_HUNK_FAILED);
498 goto done;
500 if (mangled)
501 h->ws_mangled = 1;
502 break;
506 done:
507 free(line);
508 return err;
511 static const struct got_error *
512 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
513 off_t from)
515 const struct got_error *err = NULL;
516 const char *t;
517 size_t linesize = 0, i, new = 0;
518 char *line = NULL;
519 char mode;
520 ssize_t linelen;
522 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
523 return got_error_from_errno("fseeko");
525 for (i = 0; i < h->len; ++i) {
526 switch (mode = *h->lines[i]) {
527 case '-':
528 case ' ':
529 (*lineno)++;
530 if (orig != NULL) {
531 linelen = getline(&line, &linesize, orig);
532 if (linelen == -1) {
533 err = got_error_from_errno("getline");
534 goto done;
536 if (line[linelen - 1] == '\n')
537 line[linelen - 1] = '\0';
538 t = line;
539 } else
540 t = h->lines[i] + 1;
541 if (mode == '-')
542 continue;
543 if (fprintf(tmp, "%s\n", t) < 0) {
544 err = got_error_from_errno("fprintf");
545 goto done;
547 break;
548 case '+':
549 new++;
550 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
551 err = got_error_from_errno("fprintf");
552 goto done;
554 if (new != h->new_lines || !h->new_nonl) {
555 if (fprintf(tmp, "\n") < 0) {
556 err = got_error_from_errno("fprintf");
557 goto done;
560 break;
564 done:
565 free(line);
566 return err;
569 static const struct got_error *
570 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
572 const struct got_error *err = NULL;
573 struct got_patch_hunk *h;
574 struct stat sb;
575 int lineno = 0;
576 off_t copypos, pos;
577 char *line = NULL;
578 size_t linesize = 0;
579 ssize_t linelen;
581 if (p->old == NULL) { /* create */
582 h = STAILQ_FIRST(&p->head);
583 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
584 return got_error(GOT_ERR_PATCH_MALFORMED);
585 return apply_hunk(orig, tmp, h, &lineno, 0);
588 if (fstat(fileno(orig), &sb) == -1)
589 return got_error_from_errno("fstat");
591 copypos = 0;
592 STAILQ_FOREACH(h, &p->head, entries) {
593 tryagain:
594 err = locate_hunk(orig, h, &pos, &lineno);
595 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
596 h->err = err;
597 if (err != NULL)
598 return err;
599 err = copy(tmp, orig, copypos, pos);
600 if (err != NULL)
601 return err;
602 copypos = pos;
604 err = test_hunk(orig, h);
605 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
606 /*
607 * try to apply the hunk again starting the search
608 * after the previous partial match.
609 */
610 if (fseeko(orig, pos, SEEK_SET) == -1)
611 return got_error_from_errno("fseeko");
612 linelen = getline(&line, &linesize, orig);
613 if (linelen == -1)
614 return got_error_from_errno("getline");
615 lineno++;
616 goto tryagain;
618 if (err != NULL)
619 return err;
621 if (lineno + 1 != h->old_from)
622 h->offset = lineno + 1 - h->old_from;
624 err = apply_hunk(orig, tmp, h, &lineno, pos);
625 if (err != NULL)
626 return err;
628 copypos = ftello(orig);
629 if (copypos == -1)
630 return got_error_from_errno("ftello");
633 if (p->new == NULL && sb.st_size != copypos) {
634 h = STAILQ_FIRST(&p->head);
635 h->err = got_error(GOT_ERR_HUNK_FAILED);
636 err = h->err;
637 } else if (!feof(orig))
638 err = copy(tmp, orig, copypos, -1);
640 return err;
643 static const struct got_error *
644 report_progress(struct patch_args *pa, const char *old, const char *new,
645 unsigned char status, const struct got_error *orig_error)
647 const struct got_error *err;
648 struct got_patch_hunk *h;
650 err = pa->progress_cb(pa->progress_arg, old, new, status,
651 orig_error, 0, 0, 0, 0, 0, 0, NULL);
652 if (err)
653 return err;
655 STAILQ_FOREACH(h, pa->head, entries) {
656 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
657 continue;
659 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
660 h->old_from, h->old_lines, h->new_from, h->new_lines,
661 h->offset, h->ws_mangled, h->err);
662 if (err)
663 return err;
666 return NULL;
669 static const struct got_error *
670 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
671 const char *path)
673 return report_progress(arg, path, NULL, status, NULL);
676 static const struct got_error *
677 patch_add(void *arg, unsigned char status, const char *path)
679 return report_progress(arg, NULL, path, status, NULL);
682 static const struct got_error *
683 open_blob(char **path, FILE **fp, const char *blobid,
684 struct got_repository *repo)
686 const struct got_error *err = NULL;
687 struct got_blob_object *blob = NULL;
688 struct got_object_id id, *idptr, *matched_id = NULL;
689 int fd = -1;
691 *fp = NULL;
692 *path = NULL;
694 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
695 err = got_repo_match_object_id(&matched_id, NULL, blobid,
696 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
697 repo);
698 if (err)
699 return err;
700 idptr = matched_id;
701 } else {
702 if (!got_parse_sha1_digest(id.sha1, blobid))
703 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
704 idptr = &id;
707 fd = got_opentempfd();
708 if (fd == -1) {
709 err = got_error_from_errno("got_opentempfd");
710 goto done;
713 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
714 if (err)
715 goto done;
717 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
718 "");
719 if (err)
720 goto done;
722 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
723 if (err)
724 goto done;
726 done:
727 if (fd != -1 && close(fd) == -1 && err == NULL)
728 err = got_error_from_errno("close");
729 if (blob)
730 got_object_blob_close(blob);
731 if (matched_id != NULL)
732 free(matched_id);
733 if (err) {
734 if (*fp != NULL)
735 fclose(*fp);
736 if (*path != NULL)
737 unlink(*path);
738 free(*path);
739 *fp = NULL;
740 *path = NULL;
742 return err;
745 static const struct got_error *
746 prepare_merge(int *do_merge, char **apath, FILE **afile,
747 struct got_worktree *worktree, struct got_repository *repo,
748 struct got_patch *p, struct got_object_id *commit_id,
749 struct got_tree_object *tree, const char *path)
751 const struct got_error *err = NULL;
753 *do_merge = 0;
754 *apath = NULL;
755 *afile = NULL;
757 /* don't run the diff3 merge on creations/deletions */
758 if (p->old == NULL || p->new == NULL)
759 return NULL;
761 if (commit_id) {
762 struct got_object_id *id;
764 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
765 if (err)
766 return err;
767 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
768 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
769 free(id);
770 err = open_blob(apath, afile, p->blob, repo);
771 *do_merge = err == NULL;
772 } else if (*p->blob != '\0') {
773 err = open_blob(apath, afile, p->blob, repo);
774 /*
775 * ignore failures to open this blob, we might have
776 * parsed gibberish.
777 */
778 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
779 err->code != GOT_ERR_NO_OBJ)
780 return err;
781 *do_merge = err == NULL;
782 err = NULL;
785 return err;
788 static const struct got_error *
789 apply_patch(int *overlapcnt, struct got_worktree *worktree,
790 struct got_repository *repo, struct got_fileindex *fileindex,
791 const char *old, const char *new, struct got_patch *p, int nop,
792 int reverse, struct got_object_id *commit_id,
793 struct got_tree_object *tree, struct patch_args *pa,
794 got_cancel_cb cancel_cb, void *cancel_arg)
796 const struct got_error *err = NULL;
797 struct stat sb;
798 int do_merge = 0, file_renamed = 0;
799 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
800 char *oldpath = NULL, *newpath = NULL;
801 char *tmppath = NULL, *template = NULL;
802 char *apath = NULL, *mergepath = NULL;
803 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
804 int outfd;
805 mode_t mode = GOT_DEFAULT_FILE_MODE;
807 *overlapcnt = 0;
809 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
810 commit_id, tree, old);
811 if (err)
812 return err;
814 if (reverse && !do_merge)
815 reverse_patch(p);
817 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
818 old) == -1) {
819 err = got_error_from_errno("asprintf");
820 goto done;
823 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
824 new) == -1) {
825 err = got_error_from_errno("asprintf");
826 goto done;
829 file_renamed = strcmp(oldpath, newpath);
831 if (asprintf(&template, "%s/got-patch",
832 got_worktree_get_root_path(worktree)) == -1) {
833 err = got_error_from_errno(template);
834 goto done;
837 if (p->old != NULL) {
838 if ((oldfile = fopen(oldpath, "r")) == NULL) {
839 err = got_error_from_errno2("open", oldpath);
840 goto done;
842 if (fstat(fileno(oldfile), &sb) == -1) {
843 err = got_error_from_errno2("fstat", oldpath);
844 goto done;
846 mode = sb.st_mode;
847 } else if (p->xbit)
848 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
850 err = got_opentemp_named(&tmppath, &tmpfile, template, "");
851 if (err)
852 goto done;
853 outfd = fileno(tmpfile);
854 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
855 if (err)
856 goto done;
858 if (do_merge) {
859 const char *type, *id;
861 if (fseeko(afile, 0, SEEK_SET) == -1 ||
862 fseeko(oldfile, 0, SEEK_SET) == -1 ||
863 fseeko(tmpfile, 0, SEEK_SET) == -1) {
864 err = got_error_from_errno("fseeko");
865 goto done;
868 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
869 err = got_error_from_errno("asprintf");
870 oldlabel = NULL;
871 goto done;
874 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
875 err = got_error_from_errno("asprintf");
876 newlabel = NULL;
877 goto done;
880 if (*p->cid != '\0') {
881 type = "commit";
882 id = p->cid;
883 } else {
884 type = "blob";
885 id = p->blob;
888 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
889 err = got_error_from_errno("asprintf");
890 anclabel = NULL;
891 goto done;
894 if (reverse) {
895 char *s;
896 FILE *t;
898 s = anclabel;
899 anclabel = newlabel;
900 newlabel = s;
902 t = afile;
903 afile = tmpfile;
904 tmpfile = t;
907 err = got_opentemp_named(&mergepath, &mergefile, template, "");
908 if (err)
909 goto done;
910 outfd = fileno(mergefile);
912 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
913 oldfile, oldlabel, anclabel, newlabel,
914 GOT_DIFF_ALGORITHM_PATIENCE);
915 if (err)
916 goto done;
919 if (nop)
920 goto done;
922 if (p->old != NULL && p->new == NULL) {
923 err = got_worktree_patch_schedule_rm(old, repo, worktree,
924 fileindex, patch_delete, pa);
925 goto done;
928 if (fchmod(outfd, apply_umask(mode)) == -1) {
929 err = got_error_from_errno2("chmod", tmppath);
930 goto done;
933 if (mergepath) {
934 err = got_path_move_file(mergepath, newpath);
935 if (err)
936 goto done;
937 free(mergepath);
938 mergepath = NULL;
939 } else {
940 err = got_path_move_file(tmppath, newpath);
941 if (err)
942 goto done;
943 free(tmppath);
944 tmppath = NULL;
947 if (file_renamed) {
948 err = got_worktree_patch_schedule_rm(old, repo, worktree,
949 fileindex, patch_delete, pa);
950 if (err == NULL)
951 err = got_worktree_patch_schedule_add(new, repo,
952 worktree, fileindex, patch_add,
953 pa);
954 if (err)
955 unlink(newpath);
956 } else if (p->old == NULL) {
957 err = got_worktree_patch_schedule_add(new, repo, worktree,
958 fileindex, patch_add, pa);
959 if (err)
960 unlink(newpath);
961 } else if (*overlapcnt != 0)
962 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
963 else if (do_merge)
964 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
965 else
966 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
968 done:
969 free(template);
971 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
972 err = got_error_from_errno("unlink");
973 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
974 err = got_error_from_errno("fclose");
975 free(tmppath);
977 free(oldpath);
978 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
979 err = got_error_from_errno("fclose");
981 if (apath != NULL && unlink(apath) == -1 && err == NULL)
982 err = got_error_from_errno("unlink");
983 if (afile != NULL && fclose(afile) == EOF && err == NULL)
984 err = got_error_from_errno("fclose");
985 free(apath);
987 if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
988 err = got_error_from_errno("unlink");
989 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
990 err = got_error_from_errno("fclose");
991 free(mergepath);
993 free(newpath);
994 free(oldlabel);
995 free(newlabel);
996 free(anclabel);
997 return err;
1000 const struct got_error *
1001 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1002 int nop, int strip, int reverse, struct got_object_id *commit_id,
1003 got_patch_progress_cb progress_cb, void *progress_arg,
1004 got_cancel_cb cancel_cb, void *cancel_arg)
1006 const struct got_error *err = NULL, *complete_err = NULL;
1007 struct got_fileindex *fileindex = NULL;
1008 struct got_commit_object *commit = NULL;
1009 struct got_tree_object *tree = NULL;
1010 char *fileindex_path = NULL;
1011 char *oldpath, *newpath;
1012 struct imsgbuf *ibuf;
1013 int imsg_fds[2] = {-1, -1};
1014 int overlapcnt, done = 0, failed = 0;
1015 pid_t pid;
1017 ibuf = calloc(1, sizeof(*ibuf));
1018 if (ibuf == NULL) {
1019 err = got_error_from_errno("calloc");
1020 goto done;
1023 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1024 err = got_error_from_errno("socketpair");
1025 goto done;
1028 pid = fork();
1029 if (pid == -1) {
1030 err = got_error_from_errno("fork");
1031 goto done;
1032 } else if (pid == 0) {
1033 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1034 NULL);
1035 /* not reached */
1038 if (close(imsg_fds[1]) == -1) {
1039 err = got_error_from_errno("close");
1040 goto done;
1042 imsg_fds[1] = -1;
1043 imsg_init(ibuf, imsg_fds[0]);
1045 err = send_patch(ibuf, fd);
1046 fd = -1;
1047 if (err)
1048 goto done;
1050 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1051 worktree);
1052 if (err)
1053 goto done;
1055 if (commit_id) {
1056 err = got_object_open_as_commit(&commit, repo, commit_id);
1057 if (err)
1058 goto done;
1060 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1061 if (err)
1062 goto done;
1065 while (!done && err == NULL) {
1066 struct got_patch p;
1067 struct patch_args pa;
1069 pa.progress_cb = progress_cb;
1070 pa.progress_arg = progress_arg;
1071 pa.head = &p.head;
1073 err = recv_patch(ibuf, &done, &p, strip);
1074 if (err || done)
1075 break;
1077 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1078 &newpath, worktree, repo, fileindex);
1079 if (err == NULL)
1080 err = apply_patch(&overlapcnt, worktree, repo,
1081 fileindex, oldpath, newpath, &p, nop, reverse,
1082 commit_id, tree, &pa, cancel_cb, cancel_arg);
1083 if (err != NULL) {
1084 failed = 1;
1085 /* recoverable errors */
1086 if (err->code == GOT_ERR_FILE_STATUS ||
1087 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1088 err = report_progress(&pa, p.old, p.new,
1089 GOT_STATUS_CANNOT_UPDATE, err);
1090 else if (err->code == GOT_ERR_HUNK_FAILED)
1091 err = report_progress(&pa, p.old, p.new,
1092 GOT_STATUS_CANNOT_UPDATE, NULL);
1094 if (overlapcnt != 0)
1095 failed = 1;
1097 free(oldpath);
1098 free(newpath);
1099 patch_free(&p);
1101 if (err)
1102 break;
1105 done:
1106 if (fileindex != NULL)
1107 complete_err = got_worktree_patch_complete(fileindex,
1108 fileindex_path);
1109 if (complete_err && err == NULL)
1110 err = complete_err;
1111 free(fileindex_path);
1112 if (tree)
1113 got_object_tree_close(tree);
1114 if (commit)
1115 got_object_commit_close(commit);
1116 if (fd != -1 && close(fd) == -1 && err == NULL)
1117 err = got_error_from_errno("close");
1118 if (ibuf != NULL)
1119 imsg_clear(ibuf);
1120 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1121 err = got_error_from_errno("close");
1122 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1123 err = got_error_from_errno("close");
1124 if (err == NULL && failed)
1125 err = got_error(GOT_ERR_PATCH_FAILED);
1126 return err;