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.
18 * Things that we may want to support:
19 * + support indented patches?
20 * + support other kinds of patches?
23 #include <sys/types.h>
24 #include <sys/queue.h>
25 #include <sys/socket.h>
40 #include "got_error.h"
41 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_repository.h"
47 #include "got_opentemp.h"
48 #include "got_patch.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_diff.h"
53 #include "got_lib_object.h"
54 #include "got_lib_privsep.h"
55 #include "got_lib_sha1.h"
57 #define MIN(a, b) ((a) < (b) ? (a) : (b))
59 struct got_patch_hunk {
60 STAILQ_ENTRY(got_patch_hunk) entries;
61 const struct got_error *err;
75 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
81 struct got_patch_hunk_head head;
85 got_patch_progress_cb progress_cb;
87 struct got_patch_hunk_head *head;
90 static const struct got_error *
91 send_patch(struct imsgbuf *ibuf, int fd)
93 const struct got_error *err = NULL;
95 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
97 err = got_error_from_errno(
98 "imsg_compose GOT_IMSG_PATCH_FILE");
103 return got_privsep_flush_imsg(ibuf);
107 patch_free(struct got_patch *p)
109 struct got_patch_hunk *h;
112 while (!STAILQ_EMPTY(&p->head)) {
113 h = STAILQ_FIRST(&p->head);
114 STAILQ_REMOVE_HEAD(&p->head, entries);
116 for (i = 0; i < h->len; ++i)
125 memset(p, 0, sizeof(*p));
126 STAILQ_INIT(&p->head);
129 static const struct got_error *
130 pushline(struct got_patch_hunk *h, const char *line)
135 if (h->len == h->cap) {
136 if ((newcap = h->cap * 1.5) == 0)
138 t = recallocarray(h->lines, h->cap, newcap,
139 sizeof(h->lines[0]));
141 return got_error_from_errno("recallocarray");
146 if ((t = strdup(line)) == NULL)
147 return got_error_from_errno("strdup");
149 h->lines[h->len++] = t;
153 static const struct got_error *
154 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
156 const struct got_error *err = NULL;
158 struct got_imsg_patch_hunk hdr;
159 struct got_imsg_patch patch;
160 struct got_patch_hunk *h = NULL;
164 memset(p, 0, sizeof(*p));
165 STAILQ_INIT(&p->head);
167 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
170 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
174 if (imsg.hdr.type != GOT_IMSG_PATCH) {
175 err = got_error(GOT_ERR_PRIVSEP_MSG);
178 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
179 if (datalen != sizeof(patch)) {
180 err = got_error(GOT_ERR_PRIVSEP_LEN);
183 memcpy(&patch, imsg.data, sizeof(patch));
185 if (patch.old[sizeof(patch.old)-1] != '\0' ||
186 patch.new[sizeof(patch.new)-1] != '\0' ||
187 patch.cid[sizeof(patch.cid)-1] != '\0' ||
188 patch.blob[sizeof(patch.blob)-1] != '\0') {
189 err = got_error(GOT_ERR_PRIVSEP_LEN);
193 if (*patch.cid != '\0')
194 strlcpy(p->cid, patch.cid, sizeof(p->cid));
196 if (*patch.blob != '\0')
197 strlcpy(p->blob, patch.blob, sizeof(p->blob));
199 /* automatically set strip=1 for git-style diffs */
200 if (strip == -1 && patch.git &&
201 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
202 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
205 /* prefer the new name if not /dev/null for not git-style diffs */
206 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
207 err = got_path_strip(&p->old, patch.new, strip);
210 } else if (*patch.old != '\0') {
211 err = got_path_strip(&p->old, patch.old, strip);
216 if (*patch.new != '\0') {
217 err = got_path_strip(&p->new, patch.new, strip);
222 if (p->old == NULL && p->new == NULL) {
223 err = got_error(GOT_ERR_PATCH_MALFORMED);
232 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
238 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
239 switch (imsg.hdr.type) {
240 case GOT_IMSG_PATCH_DONE:
241 if (h != NULL && h->len == 0)
242 err = got_error(GOT_ERR_PATCH_MALFORMED);
244 case GOT_IMSG_PATCH_HUNK:
246 (h->len == 0 || h->old_nonl || h->new_nonl)) {
247 err = got_error(GOT_ERR_PATCH_MALFORMED);
251 if (datalen != sizeof(hdr)) {
252 err = got_error(GOT_ERR_PRIVSEP_LEN);
255 memcpy(&hdr, imsg.data, sizeof(hdr));
256 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
257 err = got_error(GOT_ERR_PRIVSEP_LEN);
260 if ((h = calloc(1, sizeof(*h))) == NULL) {
261 err = got_error_from_errno("calloc");
264 h->old_from = hdr.oldfrom;
265 h->old_lines = hdr.oldlines;
266 h->new_from = hdr.newfrom;
267 h->new_lines = hdr.newlines;
268 STAILQ_INSERT_TAIL(&p->head, h, entries);
270 case GOT_IMSG_PATCH_LINE:
272 err = got_error(GOT_ERR_PRIVSEP_MSG);
276 /* at least one char */
277 if (datalen < 2 || t[datalen-1] != '\0') {
278 err = got_error(GOT_ERR_PRIVSEP_MSG);
281 if (*t != ' ' && *t != '-' && *t != '+' &&
283 err = got_error(GOT_ERR_PRIVSEP_MSG);
288 err = pushline(h, t);
289 else if (lastmode == '-')
291 else if (lastmode == '+')
294 err = got_error(GOT_ERR_PATCH_MALFORMED);
302 err = got_error(GOT_ERR_PRIVSEP_MSG);
318 * Copy data from orig starting at copypos until pos into tmp.
319 * If pos is -1, copy until EOF.
321 static const struct got_error *
322 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
327 if (fseeko(orig, copypos, SEEK_SET) == -1)
328 return got_error_from_errno("fseeko");
330 while (pos == -1 || copypos < pos) {
333 len = MIN(len, (size_t)pos - copypos);
334 r = fread(buf, 1, len, orig);
335 if (r != len && ferror(orig))
336 return got_error_from_errno("fread");
337 w = fwrite(buf, 1, r, tmp);
339 return got_error_from_errno("fwrite");
341 if (r != len && feof(orig)) {
344 return got_error(GOT_ERR_HUNK_FAILED);
350 static const struct got_error *
351 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
353 const struct got_error *err = NULL;
355 char mode = *h->lines[0];
359 int match_lineno = -1;
362 linelen = getline(&line, &linesize, orig);
365 err = got_error_from_errno("getline");
366 else if (match == -1)
367 err = got_error(GOT_ERR_HUNK_FAILED);
370 if (line[linelen - 1] == '\n')
371 line[linelen - 1] = '\0';
374 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
375 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
376 (mode == '+' && *lineno == h->old_from)) {
377 match = ftello(orig);
379 err = got_error_from_errno("ftello");
383 match_lineno = (*lineno)-1;
386 if (*lineno >= h->old_from && match != -1)
392 *lineno = match_lineno;
393 if (fseeko(orig, match, SEEK_SET) == -1)
394 err = got_error_from_errno("fseeko");
402 linecmp(const char *a, const char *b, int *mangled)
413 while (*a == '\t' || *a == ' ' || *a == '\f')
415 while (*b == '\t' || *b == ' ' || *b == '\f')
417 if (*a == '\0' || *a != *b)
425 static const struct got_error *
426 test_hunk(FILE *orig, struct got_patch_hunk *h)
428 const struct got_error *err = NULL;
430 size_t linesize = 0, i = 0;
434 for (i = 0; i < h->len; ++i) {
435 switch (*h->lines[i]) {
440 linelen = getline(&line, &linesize, orig);
443 err = got_error_from_errno("getline");
446 GOT_ERR_HUNK_FAILED);
449 if (line[linelen - 1] == '\n')
450 line[linelen - 1] = '\0';
451 if (linecmp(h->lines[i] + 1, line, &mangled)) {
452 err = got_error(GOT_ERR_HUNK_FAILED);
466 static const struct got_error *
467 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
470 const struct got_error *err = NULL;
472 size_t linesize = 0, i, new = 0;
477 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
478 return got_error_from_errno("fseeko");
480 for (i = 0; i < h->len; ++i) {
481 switch (mode = *h->lines[i]) {
486 linelen = getline(&line, &linesize, orig);
488 err = got_error_from_errno("getline");
491 if (line[linelen - 1] == '\n')
492 line[linelen - 1] = '\0';
498 if (fprintf(tmp, "%s\n", t) < 0) {
499 err = got_error_from_errno("fprintf");
505 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
506 err = got_error_from_errno("fprintf");
509 if (new != h->new_lines || !h->new_nonl) {
510 if (fprintf(tmp, "\n") < 0) {
511 err = got_error_from_errno("fprintf");
524 static const struct got_error *
525 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
527 const struct got_error *err = NULL;
528 struct got_patch_hunk *h;
536 if (p->old == NULL) { /* create */
537 h = STAILQ_FIRST(&p->head);
538 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
539 return got_error(GOT_ERR_PATCH_MALFORMED);
540 return apply_hunk(orig, tmp, h, &lineno, 0);
543 if (fstat(fileno(orig), &sb) == -1)
544 return got_error_from_errno("fstat");
547 STAILQ_FOREACH(h, &p->head, entries) {
549 err = locate_hunk(orig, h, &pos, &lineno);
550 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
554 err = copy(tmp, orig, copypos, pos);
559 err = test_hunk(orig, h);
560 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
562 * try to apply the hunk again starting the search
563 * after the previous partial match.
565 if (fseeko(orig, pos, SEEK_SET) == -1)
566 return got_error_from_errno("fseeko");
567 linelen = getline(&line, &linesize, orig);
569 return got_error_from_errno("getline");
576 if (lineno + 1 != h->old_from)
577 h->offset = lineno + 1 - h->old_from;
579 err = apply_hunk(orig, tmp, h, &lineno, pos);
583 copypos = ftello(orig);
585 return got_error_from_errno("ftello");
588 if (p->new == NULL && sb.st_size != copypos) {
589 h = STAILQ_FIRST(&p->head);
590 h->err = got_error(GOT_ERR_HUNK_FAILED);
592 } else if (!feof(orig))
593 err = copy(tmp, orig, copypos, -1);
598 static const struct got_error *
599 report_progress(struct patch_args *pa, const char *old, const char *new,
600 unsigned char status, const struct got_error *orig_error)
602 const struct got_error *err;
603 struct got_patch_hunk *h;
605 err = pa->progress_cb(pa->progress_arg, old, new, status,
606 orig_error, 0, 0, 0, 0, 0, 0, NULL);
610 STAILQ_FOREACH(h, pa->head, entries) {
611 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
614 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
615 h->old_from, h->old_lines, h->new_from, h->new_lines,
616 h->offset, h->ws_mangled, h->err);
624 static const struct got_error *
625 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
628 return report_progress(arg, path, NULL, status, NULL);
631 static const struct got_error *
632 patch_add(void *arg, unsigned char status, const char *path)
634 return report_progress(arg, NULL, path, status, NULL);
637 static const struct got_error *
638 open_blob(char **path, FILE **fp, const char *blobid,
639 struct got_repository *repo)
641 const struct got_error *err = NULL;
642 struct got_blob_object *blob = NULL;
643 struct got_object_id id, *idptr, *matched_id = NULL;
649 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
650 err = got_repo_match_object_id(&matched_id, NULL, blobid,
651 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
657 if (!got_parse_sha1_digest(id.sha1, blobid))
658 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
662 fd = got_opentempfd();
664 err = got_error_from_errno("got_opentempfd");
668 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
672 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
676 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
681 if (fd != -1 && close(fd) == -1 && err == NULL)
682 err = got_error_from_errno("close");
684 got_object_blob_close(blob);
685 if (matched_id != NULL)
699 static const struct got_error *
700 apply_patch(int *overlapcnt, struct got_worktree *worktree,
701 struct got_repository *repo, struct got_fileindex *fileindex,
702 const char *old, const char *new, struct got_patch *p, int nop,
703 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
705 const struct got_error *err = NULL;
707 int do_merge = 0, file_renamed = 0;
708 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
709 char *oldpath = NULL, *newpath = NULL;
710 char *tmppath = NULL, *template = NULL, *parent = NULL;
711 char *apath = NULL, *mergepath = NULL;
712 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
715 mode_t mode = GOT_DEFAULT_FILE_MODE;
719 /* don't run the diff3 merge on creations/deletions */
720 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
721 err = open_blob(&apath, &afile, p->blob, repo);
723 * ignore failures to open this blob, we might have
726 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
727 err->code != GOT_ERR_NO_OBJ)
729 else if (err == NULL)
734 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
736 err = got_error_from_errno("asprintf");
740 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
742 err = got_error_from_errno("asprintf");
746 file_renamed = strcmp(oldpath, newpath);
748 if (asprintf(&template, "%s/got-patch",
749 got_worktree_get_root_path(worktree)) == -1) {
750 err = got_error_from_errno(template);
754 if (p->old != NULL) {
755 if ((oldfile = fopen(oldpath, "r")) == NULL) {
756 err = got_error_from_errno2("open", oldpath);
759 if (fstat(fileno(oldfile), &sb) == -1) {
760 err = got_error_from_errno2("fstat", oldpath);
766 err = got_opentemp_named(&tmppath, &tmpfile, template);
770 outfd = fileno(tmpfile);
771 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
776 const char *type, *id;
778 if (fseeko(afile, 0, SEEK_SET) == -1 ||
779 fseeko(oldfile, 0, SEEK_SET) == -1 ||
780 fseeko(tmpfile, 0, SEEK_SET) == -1) {
781 err = got_error_from_errno("fseeko");
785 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
786 err = got_error_from_errno("asprintf");
791 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
792 err = got_error_from_errno("asprintf");
797 if (*p->cid != '\0') {
805 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
806 err = got_error_from_errno("asprintf");
811 err = got_opentemp_named(&mergepath, &mergefile, template);
815 outfd = fileno(mergefile);
817 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
818 oldfile, oldlabel, anclabel, newlabel,
819 GOT_DIFF_ALGORITHM_PATIENCE);
827 if (p->old != NULL && p->new == NULL) {
828 err = got_worktree_patch_schedule_rm(old, repo, worktree,
829 fileindex, patch_delete, pa);
833 if (fchmod(outfd, mode) == -1) {
834 err = got_error_from_errno2("chmod", tmppath);
838 if (rename(outpath, newpath) == -1) {
839 if (errno != ENOENT) {
840 err = got_error_from_errno3("rename", outpath,
845 err = got_path_dirname(&parent, newpath);
848 err = got_path_mkdir(parent);
851 if (rename(outpath, newpath) == -1) {
852 err = got_error_from_errno3("rename", outpath,
859 err = got_worktree_patch_schedule_rm(old, repo, worktree,
860 fileindex, patch_delete, pa);
862 err = got_worktree_patch_schedule_add(new, repo,
863 worktree, fileindex, patch_add,
867 } else if (p->old == NULL) {
868 err = got_worktree_patch_schedule_add(new, repo, worktree,
869 fileindex, patch_add, pa);
872 } else if (*overlapcnt != 0)
873 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
875 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
877 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
885 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
886 err = got_error_from_errno("fclose");
890 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
891 err = got_error_from_errno("fclose");
895 if (afile != NULL && fclose(afile) == EOF && err == NULL)
896 err = got_error_from_errno("fclose");
899 if (mergepath != NULL)
901 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
902 err = got_error_from_errno("fclose");
913 reverse_patch(struct got_patch *p)
915 struct got_patch_hunk *h;
919 STAILQ_FOREACH(h, &p->head, entries) {
921 h->old_from = h->new_from;
925 h->old_lines = h->new_lines;
929 h->old_nonl = h->new_nonl;
932 for (i = 0; i < h->len; ++i) {
933 if (*h->lines[i] == '+')
935 else if (*h->lines[i] == '-')
941 const struct got_error *
942 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
943 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
944 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
946 const struct got_error *err = NULL, *complete_err = NULL;
947 struct got_fileindex *fileindex = NULL;
948 char *fileindex_path = NULL;
949 char *oldpath, *newpath;
950 struct imsgbuf *ibuf;
951 int imsg_fds[2] = {-1, -1};
952 int overlapcnt, done = 0, failed = 0;
955 ibuf = calloc(1, sizeof(*ibuf));
957 err = got_error_from_errno("calloc");
961 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
962 err = got_error_from_errno("socketpair");
968 err = got_error_from_errno("fork");
970 } else if (pid == 0) {
971 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
976 if (close(imsg_fds[1]) == -1) {
977 err = got_error_from_errno("close");
981 imsg_init(ibuf, imsg_fds[0]);
983 err = send_patch(ibuf, fd);
988 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
993 while (!done && err == NULL) {
995 struct patch_args pa;
997 pa.progress_cb = progress_cb;
998 pa.progress_arg = progress_arg;
1001 err = recv_patch(ibuf, &done, &p, strip);
1008 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1009 &newpath, worktree, repo, fileindex);
1011 err = apply_patch(&overlapcnt, worktree, repo,
1012 fileindex, oldpath, newpath, &p, nop, &pa,
1013 cancel_cb, cancel_arg);
1016 /* recoverable errors */
1017 if (err->code == GOT_ERR_FILE_STATUS ||
1018 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1019 err = report_progress(&pa, p.old, p.new,
1020 GOT_STATUS_CANNOT_UPDATE, err);
1021 else if (err->code == GOT_ERR_HUNK_FAILED)
1022 err = report_progress(&pa, p.old, p.new,
1023 GOT_STATUS_CANNOT_UPDATE, NULL);
1025 if (overlapcnt != 0)
1037 if (fileindex != NULL)
1038 complete_err = got_worktree_patch_complete(fileindex,
1040 if (complete_err && err == NULL)
1042 free(fileindex_path);
1043 if (fd != -1 && close(fd) == -1 && err == NULL)
1044 err = got_error_from_errno("close");
1047 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1048 err = got_error_from_errno("close");
1049 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1050 err = got_error_from_errno("close");
1051 if (err == NULL && failed)
1052 err = got_error(GOT_ERR_PATCH_FAILED);