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 reverse_patch(struct got_patch *p)
320 struct got_patch_hunk *h;
324 STAILQ_FOREACH(h, &p->head, entries) {
326 h->old_from = h->new_from;
330 h->old_lines = h->new_lines;
334 h->old_nonl = h->new_nonl;
337 for (i = 0; i < h->len; ++i) {
338 if (*h->lines[i] == '+')
340 else if (*h->lines[i] == '-')
347 * Copy data from orig starting at copypos until pos into tmp.
348 * If pos is -1, copy until EOF.
350 static const struct got_error *
351 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
356 if (fseeko(orig, copypos, SEEK_SET) == -1)
357 return got_error_from_errno("fseeko");
359 while (pos == -1 || copypos < pos) {
362 len = MIN(len, (size_t)pos - copypos);
363 r = fread(buf, 1, len, orig);
364 if (r != len && ferror(orig))
365 return got_error_from_errno("fread");
366 w = fwrite(buf, 1, r, tmp);
368 return got_error_from_errno("fwrite");
370 if (r != len && feof(orig)) {
373 return got_error(GOT_ERR_HUNK_FAILED);
379 static const struct got_error *
380 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
382 const struct got_error *err = NULL;
384 char mode = *h->lines[0];
388 int match_lineno = -1;
391 linelen = getline(&line, &linesize, orig);
394 err = got_error_from_errno("getline");
395 else if (match == -1)
396 err = got_error(GOT_ERR_HUNK_FAILED);
399 if (line[linelen - 1] == '\n')
400 line[linelen - 1] = '\0';
403 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
404 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
405 (mode == '+' && *lineno == h->old_from)) {
406 match = ftello(orig);
408 err = got_error_from_errno("ftello");
412 match_lineno = (*lineno)-1;
415 if (*lineno >= h->old_from && match != -1)
421 *lineno = match_lineno;
422 if (fseeko(orig, match, SEEK_SET) == -1)
423 err = got_error_from_errno("fseeko");
431 linecmp(const char *a, const char *b, int *mangled)
442 while (*a == '\t' || *a == ' ' || *a == '\f')
444 while (*b == '\t' || *b == ' ' || *b == '\f')
446 if (*a == '\0' || *a != *b)
454 static const struct got_error *
455 test_hunk(FILE *orig, struct got_patch_hunk *h)
457 const struct got_error *err = NULL;
459 size_t linesize = 0, i = 0;
463 for (i = 0; i < h->len; ++i) {
464 switch (*h->lines[i]) {
469 linelen = getline(&line, &linesize, orig);
472 err = got_error_from_errno("getline");
475 GOT_ERR_HUNK_FAILED);
478 if (line[linelen - 1] == '\n')
479 line[linelen - 1] = '\0';
480 if (linecmp(h->lines[i] + 1, line, &mangled)) {
481 err = got_error(GOT_ERR_HUNK_FAILED);
495 static const struct got_error *
496 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
499 const struct got_error *err = NULL;
501 size_t linesize = 0, i, new = 0;
506 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
507 return got_error_from_errno("fseeko");
509 for (i = 0; i < h->len; ++i) {
510 switch (mode = *h->lines[i]) {
515 linelen = getline(&line, &linesize, orig);
517 err = got_error_from_errno("getline");
520 if (line[linelen - 1] == '\n')
521 line[linelen - 1] = '\0';
527 if (fprintf(tmp, "%s\n", t) < 0) {
528 err = got_error_from_errno("fprintf");
534 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
535 err = got_error_from_errno("fprintf");
538 if (new != h->new_lines || !h->new_nonl) {
539 if (fprintf(tmp, "\n") < 0) {
540 err = got_error_from_errno("fprintf");
553 static const struct got_error *
554 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
556 const struct got_error *err = NULL;
557 struct got_patch_hunk *h;
565 if (p->old == NULL) { /* create */
566 h = STAILQ_FIRST(&p->head);
567 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
568 return got_error(GOT_ERR_PATCH_MALFORMED);
569 return apply_hunk(orig, tmp, h, &lineno, 0);
572 if (fstat(fileno(orig), &sb) == -1)
573 return got_error_from_errno("fstat");
576 STAILQ_FOREACH(h, &p->head, entries) {
578 err = locate_hunk(orig, h, &pos, &lineno);
579 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
583 err = copy(tmp, orig, copypos, pos);
588 err = test_hunk(orig, h);
589 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
591 * try to apply the hunk again starting the search
592 * after the previous partial match.
594 if (fseeko(orig, pos, SEEK_SET) == -1)
595 return got_error_from_errno("fseeko");
596 linelen = getline(&line, &linesize, orig);
598 return got_error_from_errno("getline");
605 if (lineno + 1 != h->old_from)
606 h->offset = lineno + 1 - h->old_from;
608 err = apply_hunk(orig, tmp, h, &lineno, pos);
612 copypos = ftello(orig);
614 return got_error_from_errno("ftello");
617 if (p->new == NULL && sb.st_size != copypos) {
618 h = STAILQ_FIRST(&p->head);
619 h->err = got_error(GOT_ERR_HUNK_FAILED);
621 } else if (!feof(orig))
622 err = copy(tmp, orig, copypos, -1);
627 static const struct got_error *
628 report_progress(struct patch_args *pa, const char *old, const char *new,
629 unsigned char status, const struct got_error *orig_error)
631 const struct got_error *err;
632 struct got_patch_hunk *h;
634 err = pa->progress_cb(pa->progress_arg, old, new, status,
635 orig_error, 0, 0, 0, 0, 0, 0, NULL);
639 STAILQ_FOREACH(h, pa->head, entries) {
640 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
643 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
644 h->old_from, h->old_lines, h->new_from, h->new_lines,
645 h->offset, h->ws_mangled, h->err);
653 static const struct got_error *
654 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
657 return report_progress(arg, path, NULL, status, NULL);
660 static const struct got_error *
661 patch_add(void *arg, unsigned char status, const char *path)
663 return report_progress(arg, NULL, path, status, NULL);
666 static const struct got_error *
667 open_blob(char **path, FILE **fp, const char *blobid,
668 struct got_repository *repo)
670 const struct got_error *err = NULL;
671 struct got_blob_object *blob = NULL;
672 struct got_object_id id, *idptr, *matched_id = NULL;
678 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
679 err = got_repo_match_object_id(&matched_id, NULL, blobid,
680 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
686 if (!got_parse_sha1_digest(id.sha1, blobid))
687 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
691 fd = got_opentempfd();
693 err = got_error_from_errno("got_opentempfd");
697 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
701 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
705 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
710 if (fd != -1 && close(fd) == -1 && err == NULL)
711 err = got_error_from_errno("close");
713 got_object_blob_close(blob);
714 if (matched_id != NULL)
728 static const struct got_error *
729 apply_patch(int *overlapcnt, struct got_worktree *worktree,
730 struct got_repository *repo, struct got_fileindex *fileindex,
731 const char *old, const char *new, struct got_patch *p, int nop,
732 int reverse, struct patch_args *pa,
733 got_cancel_cb cancel_cb, void *cancel_arg)
735 const struct got_error *err = NULL;
737 int do_merge = 0, file_renamed = 0;
738 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
739 char *oldpath = NULL, *newpath = NULL;
740 char *tmppath = NULL, *template = NULL, *parent = NULL;
741 char *apath = NULL, *mergepath = NULL;
742 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
745 mode_t mode = GOT_DEFAULT_FILE_MODE;
749 /* don't run the diff3 merge on creations/deletions */
750 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
751 err = open_blob(&apath, &afile, p->blob, repo);
753 * ignore failures to open this blob, we might have
756 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
757 err->code != GOT_ERR_NO_OBJ)
759 else if (err == NULL)
764 if (reverse && !do_merge)
767 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
769 err = got_error_from_errno("asprintf");
773 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
775 err = got_error_from_errno("asprintf");
779 file_renamed = strcmp(oldpath, newpath);
781 if (asprintf(&template, "%s/got-patch",
782 got_worktree_get_root_path(worktree)) == -1) {
783 err = got_error_from_errno(template);
787 if (p->old != NULL) {
788 if ((oldfile = fopen(oldpath, "r")) == NULL) {
789 err = got_error_from_errno2("open", oldpath);
792 if (fstat(fileno(oldfile), &sb) == -1) {
793 err = got_error_from_errno2("fstat", oldpath);
799 err = got_opentemp_named(&tmppath, &tmpfile, template);
803 outfd = fileno(tmpfile);
804 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
809 const char *type, *id;
811 if (fseeko(afile, 0, SEEK_SET) == -1 ||
812 fseeko(oldfile, 0, SEEK_SET) == -1 ||
813 fseeko(tmpfile, 0, SEEK_SET) == -1) {
814 err = got_error_from_errno("fseeko");
818 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
819 err = got_error_from_errno("asprintf");
824 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
825 err = got_error_from_errno("asprintf");
830 if (*p->cid != '\0') {
838 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
839 err = got_error_from_errno("asprintf");
857 err = got_opentemp_named(&mergepath, &mergefile, template);
861 outfd = fileno(mergefile);
863 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
864 oldfile, oldlabel, anclabel, newlabel,
865 GOT_DIFF_ALGORITHM_PATIENCE);
873 if (p->old != NULL && p->new == NULL) {
874 err = got_worktree_patch_schedule_rm(old, repo, worktree,
875 fileindex, patch_delete, pa);
879 if (fchmod(outfd, mode) == -1) {
880 err = got_error_from_errno2("chmod", tmppath);
884 if (rename(outpath, newpath) == -1) {
885 if (errno != ENOENT) {
886 err = got_error_from_errno3("rename", outpath,
891 err = got_path_dirname(&parent, newpath);
894 err = got_path_mkdir(parent);
897 if (rename(outpath, newpath) == -1) {
898 err = got_error_from_errno3("rename", outpath,
905 err = got_worktree_patch_schedule_rm(old, repo, worktree,
906 fileindex, patch_delete, pa);
908 err = got_worktree_patch_schedule_add(new, repo,
909 worktree, fileindex, patch_add,
913 } else if (p->old == NULL) {
914 err = got_worktree_patch_schedule_add(new, repo, worktree,
915 fileindex, patch_add, pa);
918 } else if (*overlapcnt != 0)
919 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
921 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
923 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
931 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
932 err = got_error_from_errno("fclose");
936 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
937 err = got_error_from_errno("fclose");
941 if (afile != NULL && fclose(afile) == EOF && err == NULL)
942 err = got_error_from_errno("fclose");
945 if (mergepath != NULL)
947 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
948 err = got_error_from_errno("fclose");
958 const struct got_error *
959 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
960 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
961 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
963 const struct got_error *err = NULL, *complete_err = NULL;
964 struct got_fileindex *fileindex = NULL;
965 char *fileindex_path = NULL;
966 char *oldpath, *newpath;
967 struct imsgbuf *ibuf;
968 int imsg_fds[2] = {-1, -1};
969 int overlapcnt, done = 0, failed = 0;
972 ibuf = calloc(1, sizeof(*ibuf));
974 err = got_error_from_errno("calloc");
978 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
979 err = got_error_from_errno("socketpair");
985 err = got_error_from_errno("fork");
987 } else if (pid == 0) {
988 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
993 if (close(imsg_fds[1]) == -1) {
994 err = got_error_from_errno("close");
998 imsg_init(ibuf, imsg_fds[0]);
1000 err = send_patch(ibuf, fd);
1005 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1010 while (!done && err == NULL) {
1012 struct patch_args pa;
1014 pa.progress_cb = progress_cb;
1015 pa.progress_arg = progress_arg;
1018 err = recv_patch(ibuf, &done, &p, strip);
1022 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1023 &newpath, worktree, repo, fileindex);
1025 err = apply_patch(&overlapcnt, worktree, repo,
1026 fileindex, oldpath, newpath, &p, nop, reverse,
1027 &pa, cancel_cb, cancel_arg);
1030 /* recoverable errors */
1031 if (err->code == GOT_ERR_FILE_STATUS ||
1032 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1033 err = report_progress(&pa, p.old, p.new,
1034 GOT_STATUS_CANNOT_UPDATE, err);
1035 else if (err->code == GOT_ERR_HUNK_FAILED)
1036 err = report_progress(&pa, p.old, p.new,
1037 GOT_STATUS_CANNOT_UPDATE, NULL);
1039 if (overlapcnt != 0)
1051 if (fileindex != NULL)
1052 complete_err = got_worktree_patch_complete(fileindex,
1054 if (complete_err && err == NULL)
1056 free(fileindex_path);
1057 if (fd != -1 && close(fd) == -1 && err == NULL)
1058 err = got_error_from_errno("close");
1061 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1062 err = got_error_from_errno("close");
1063 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1064 err = got_error_from_errno("close");
1065 if (err == NULL && failed)
1066 err = got_error(GOT_ERR_PATCH_FAILED);