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 if (imsg_flush(ibuf) == -1) {
104 err = got_error_from_errno("imsg_flush");
112 patch_free(struct got_patch *p)
114 struct got_patch_hunk *h;
117 while (!STAILQ_EMPTY(&p->head)) {
118 h = STAILQ_FIRST(&p->head);
119 STAILQ_REMOVE_HEAD(&p->head, entries);
121 for (i = 0; i < h->len; ++i)
130 memset(p, 0, sizeof(*p));
131 STAILQ_INIT(&p->head);
134 static const struct got_error *
135 pushline(struct got_patch_hunk *h, const char *line)
140 if (h->len == h->cap) {
141 if ((newcap = h->cap * 1.5) == 0)
143 t = recallocarray(h->lines, h->cap, newcap,
144 sizeof(h->lines[0]));
146 return got_error_from_errno("recallocarray");
151 if ((t = strdup(line)) == NULL)
152 return got_error_from_errno("strdup");
154 h->lines[h->len++] = t;
158 static const struct got_error *
159 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
161 const struct got_error *err = NULL;
163 struct got_imsg_patch_hunk hdr;
164 struct got_imsg_patch patch;
165 struct got_patch_hunk *h = NULL;
169 memset(p, 0, sizeof(*p));
170 STAILQ_INIT(&p->head);
172 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
175 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
179 if (imsg.hdr.type != GOT_IMSG_PATCH) {
180 err = got_error(GOT_ERR_PRIVSEP_MSG);
183 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
184 if (datalen != sizeof(patch)) {
185 err = got_error(GOT_ERR_PRIVSEP_LEN);
188 memcpy(&patch, imsg.data, sizeof(patch));
190 if (patch.old[sizeof(patch.old)-1] != '\0' ||
191 patch.new[sizeof(patch.new)-1] != '\0' ||
192 patch.cid[sizeof(patch.cid)-1] != '\0' ||
193 patch.blob[sizeof(patch.blob)-1] != '\0') {
194 err = got_error(GOT_ERR_PRIVSEP_LEN);
198 if (*patch.cid != '\0')
199 strlcpy(p->cid, patch.cid, sizeof(p->cid));
201 if (*patch.blob != '\0')
202 strlcpy(p->blob, patch.blob, sizeof(p->blob));
204 /* automatically set strip=1 for git-style diffs */
205 if (strip == -1 && patch.git &&
206 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
207 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
210 /* prefer the new name if not /dev/null for not git-style diffs */
211 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
212 err = got_path_strip(&p->old, patch.new, strip);
215 } else if (*patch.old != '\0') {
216 err = got_path_strip(&p->old, patch.old, strip);
221 if (*patch.new != '\0') {
222 err = got_path_strip(&p->new, patch.new, strip);
227 if (p->old == NULL && p->new == NULL) {
228 err = got_error(GOT_ERR_PATCH_MALFORMED);
237 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
243 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
244 switch (imsg.hdr.type) {
245 case GOT_IMSG_PATCH_DONE:
246 if (h != NULL && h->len == 0)
247 err = got_error(GOT_ERR_PATCH_MALFORMED);
249 case GOT_IMSG_PATCH_HUNK:
251 (h->len == 0 || h->old_nonl || h->new_nonl)) {
252 err = got_error(GOT_ERR_PATCH_MALFORMED);
256 if (datalen != sizeof(hdr)) {
257 err = got_error(GOT_ERR_PRIVSEP_LEN);
260 memcpy(&hdr, imsg.data, sizeof(hdr));
261 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
262 err = got_error(GOT_ERR_PRIVSEP_LEN);
265 if ((h = calloc(1, sizeof(*h))) == NULL) {
266 err = got_error_from_errno("calloc");
269 h->old_from = hdr.oldfrom;
270 h->old_lines = hdr.oldlines;
271 h->new_from = hdr.newfrom;
272 h->new_lines = hdr.newlines;
273 STAILQ_INSERT_TAIL(&p->head, h, entries);
275 case GOT_IMSG_PATCH_LINE:
277 err = got_error(GOT_ERR_PRIVSEP_MSG);
281 /* at least one char */
282 if (datalen < 2 || t[datalen-1] != '\0') {
283 err = got_error(GOT_ERR_PRIVSEP_MSG);
286 if (*t != ' ' && *t != '-' && *t != '+' &&
288 err = got_error(GOT_ERR_PRIVSEP_MSG);
293 err = pushline(h, t);
294 else if (lastmode == '-')
296 else if (lastmode == '+')
299 err = got_error(GOT_ERR_PATCH_MALFORMED);
307 err = got_error(GOT_ERR_PRIVSEP_MSG);
323 * Copy data from orig starting at copypos until pos into tmp.
324 * If pos is -1, copy until EOF.
326 static const struct got_error *
327 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
332 if (fseeko(orig, copypos, SEEK_SET) == -1)
333 return got_error_from_errno("fseeko");
335 while (pos == -1 || copypos < pos) {
338 len = MIN(len, (size_t)pos - copypos);
339 r = fread(buf, 1, len, orig);
340 if (r != len && ferror(orig))
341 return got_error_from_errno("fread");
342 w = fwrite(buf, 1, r, tmp);
344 return got_error_from_errno("fwrite");
346 if (r != len && feof(orig)) {
349 return got_error(GOT_ERR_HUNK_FAILED);
355 static const struct got_error *
356 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
358 const struct got_error *err = NULL;
360 char mode = *h->lines[0];
364 int match_lineno = -1;
367 linelen = getline(&line, &linesize, orig);
370 err = got_error_from_errno("getline");
371 else if (match == -1)
372 err = got_error(GOT_ERR_HUNK_FAILED);
375 if (line[linelen - 1] == '\n')
376 line[linelen - 1] = '\0';
379 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
380 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
381 (mode == '+' && *lineno == h->old_from)) {
382 match = ftello(orig);
384 err = got_error_from_errno("ftello");
388 match_lineno = (*lineno)-1;
391 if (*lineno >= h->old_from && match != -1)
397 *lineno = match_lineno;
398 if (fseeko(orig, match, SEEK_SET) == -1)
399 err = got_error_from_errno("fseeko");
407 linecmp(const char *a, const char *b, int *mangled)
422 if (*a == '\0' || *b == '\0' || *a != *b)
430 static const struct got_error *
431 test_hunk(FILE *orig, struct got_patch_hunk *h)
433 const struct got_error *err = NULL;
435 size_t linesize = 0, i = 0;
439 for (i = 0; i < h->len; ++i) {
440 switch (*h->lines[i]) {
445 linelen = getline(&line, &linesize, orig);
448 err = got_error_from_errno("getline");
451 GOT_ERR_HUNK_FAILED);
454 if (line[linelen - 1] == '\n')
455 line[linelen - 1] = '\0';
456 if (linecmp(h->lines[i] + 1, line, &mangled)) {
457 err = got_error(GOT_ERR_HUNK_FAILED);
471 static const struct got_error *
472 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
475 const struct got_error *err = NULL;
477 size_t linesize = 0, i, new = 0;
482 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
483 return got_error_from_errno("fseeko");
485 for (i = 0; i < h->len; ++i) {
486 switch (mode = *h->lines[i]) {
491 linelen = getline(&line, &linesize, orig);
493 err = got_error_from_errno("getline");
496 if (line[linelen - 1] == '\n')
497 line[linelen - 1] = '\0';
503 if (fprintf(tmp, "%s\n", t) < 0) {
504 err = got_error_from_errno("fprintf");
510 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
511 err = got_error_from_errno("fprintf");
514 if (new != h->new_lines || !h->new_nonl) {
515 if (fprintf(tmp, "\n") < 0) {
516 err = got_error_from_errno("fprintf");
529 static const struct got_error *
530 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
532 const struct got_error *err = NULL;
533 struct got_patch_hunk *h;
541 if (p->old == NULL) { /* create */
542 h = STAILQ_FIRST(&p->head);
543 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
544 return got_error(GOT_ERR_PATCH_MALFORMED);
545 return apply_hunk(orig, tmp, h, &lineno, 0);
548 if (fstat(fileno(orig), &sb) == -1)
549 return got_error_from_errno("fstat");
552 STAILQ_FOREACH(h, &p->head, entries) {
554 err = locate_hunk(orig, h, &pos, &lineno);
555 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
559 err = copy(tmp, orig, copypos, pos);
564 err = test_hunk(orig, h);
565 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
567 * try to apply the hunk again starting the search
568 * after the previous partial match.
570 if (fseeko(orig, pos, SEEK_SET) == -1)
571 return got_error_from_errno("fseeko");
572 linelen = getline(&line, &linesize, orig);
574 return got_error_from_errno("getline");
581 if (lineno + 1 != h->old_from)
582 h->offset = lineno + 1 - h->old_from;
584 err = apply_hunk(orig, tmp, h, &lineno, pos);
588 copypos = ftello(orig);
590 return got_error_from_errno("ftello");
593 if (p->new == NULL && sb.st_size != copypos) {
594 h = STAILQ_FIRST(&p->head);
595 h->err = got_error(GOT_ERR_HUNK_FAILED);
597 } else if (!feof(orig))
598 err = copy(tmp, orig, copypos, -1);
603 static const struct got_error *
604 report_progress(struct patch_args *pa, const char *old, const char *new,
605 unsigned char status, const struct got_error *orig_error)
607 const struct got_error *err;
608 struct got_patch_hunk *h;
610 err = pa->progress_cb(pa->progress_arg, old, new, status,
611 orig_error, 0, 0, 0, 0, 0, 0, NULL);
615 STAILQ_FOREACH(h, pa->head, entries) {
616 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
619 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
620 h->old_from, h->old_lines, h->new_from, h->new_lines,
621 h->offset, h->ws_mangled, h->err);
629 static const struct got_error *
630 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
633 return report_progress(arg, path, NULL, status, NULL);
636 static const struct got_error *
637 patch_add(void *arg, unsigned char status, const char *path)
639 return report_progress(arg, NULL, path, status, NULL);
642 static const struct got_error *
643 open_blob(char **path, FILE **fp, const char *blobid,
644 struct got_repository *repo)
646 const struct got_error *err = NULL;
647 struct got_blob_object *blob = NULL;
648 struct got_object_id id, *idptr, *matched_id = NULL;
654 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
655 err = got_repo_match_object_id(&matched_id, NULL, blobid,
656 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
662 if (!got_parse_sha1_digest(id.sha1, blobid))
663 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
667 fd = got_opentempfd();
669 err = got_error_from_errno("got_opentempfd");
673 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
677 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
681 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
686 if (fd != -1 && close(fd) == -1 && err == NULL)
687 err = got_error_from_errno("close");
689 got_object_blob_close(blob);
690 if (matched_id != NULL)
704 static const struct got_error *
705 apply_patch(int *overlapcnt, struct got_worktree *worktree,
706 struct got_repository *repo, struct got_fileindex *fileindex,
707 const char *old, const char *new, struct got_patch *p, int nop,
708 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
710 const struct got_error *err = NULL;
712 int do_merge = 0, file_renamed = 0;
713 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
714 char *oldpath = NULL, *newpath = NULL;
715 char *tmppath = NULL, *template = NULL, *parent = NULL;
716 char *apath = NULL, *mergepath = NULL;
717 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
720 mode_t mode = GOT_DEFAULT_FILE_MODE;
724 /* don't run the diff3 merge on creations/deletions */
725 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
726 err = open_blob(&apath, &afile, p->blob, repo);
728 * ignore failures to open this blob, we might have
731 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
732 err->code != GOT_ERR_NO_OBJ)
734 else if (err == NULL)
739 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
741 err = got_error_from_errno("asprintf");
745 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
747 err = got_error_from_errno("asprintf");
751 file_renamed = strcmp(oldpath, newpath);
753 if (asprintf(&template, "%s/got-patch",
754 got_worktree_get_root_path(worktree)) == -1) {
755 err = got_error_from_errno(template);
759 if (p->old != NULL) {
760 if ((oldfile = fopen(oldpath, "r")) == NULL) {
761 err = got_error_from_errno2("open", oldpath);
764 if (fstat(fileno(oldfile), &sb) == -1) {
765 err = got_error_from_errno2("fstat", oldpath);
771 err = got_opentemp_named(&tmppath, &tmpfile, template);
775 outfd = fileno(tmpfile);
776 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
781 const char *type, *id;
783 if (fseeko(afile, 0, SEEK_SET) == -1 ||
784 fseeko(oldfile, 0, SEEK_SET) == -1 ||
785 fseeko(tmpfile, 0, SEEK_SET) == -1) {
786 err = got_error_from_errno("fseeko");
790 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
791 err = got_error_from_errno("asprintf");
796 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
797 err = got_error_from_errno("asprintf");
802 if (*p->cid != '\0') {
810 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
811 err = got_error_from_errno("asprintf");
816 err = got_opentemp_named(&mergepath, &mergefile, template);
820 outfd = fileno(mergefile);
822 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
823 oldfile, oldlabel, anclabel, newlabel,
824 GOT_DIFF_ALGORITHM_PATIENCE);
832 if (p->old != NULL && p->new == NULL) {
833 err = got_worktree_patch_schedule_rm(old, repo, worktree,
834 fileindex, patch_delete, pa);
838 if (fchmod(outfd, mode) == -1) {
839 err = got_error_from_errno2("chmod", tmppath);
843 if (rename(outpath, newpath) == -1) {
844 if (errno != ENOENT) {
845 err = got_error_from_errno3("rename", outpath,
850 err = got_path_dirname(&parent, newpath);
853 err = got_path_mkdir(parent);
856 if (rename(outpath, newpath) == -1) {
857 err = got_error_from_errno3("rename", outpath,
864 err = got_worktree_patch_schedule_rm(old, repo, worktree,
865 fileindex, patch_delete, pa);
867 err = got_worktree_patch_schedule_add(new, repo,
868 worktree, fileindex, patch_add,
872 } else if (p->old == NULL) {
873 err = got_worktree_patch_schedule_add(new, repo, worktree,
874 fileindex, patch_add, pa);
877 } else if (*overlapcnt != 0)
878 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
880 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
882 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
890 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
891 err = got_error_from_errno("fclose");
895 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
896 err = got_error_from_errno("fclose");
900 if (afile != NULL && fclose(afile) == EOF && err == NULL)
901 err = got_error_from_errno("fclose");
904 if (mergepath != NULL)
906 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
907 err = got_error_from_errno("fclose");
918 reverse_patch(struct got_patch *p)
920 struct got_patch_hunk *h;
924 STAILQ_FOREACH(h, &p->head, entries) {
926 h->old_from = h->new_from;
930 h->old_lines = h->new_lines;
934 h->old_nonl = h->new_nonl;
937 for (i = 0; i < h->len; ++i) {
938 if (*h->lines[i] == '+')
940 else if (*h->lines[i] == '-')
946 const struct got_error *
947 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
948 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
949 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
951 const struct got_error *err = NULL, *complete_err = NULL;
952 struct got_fileindex *fileindex = NULL;
953 char *fileindex_path = NULL;
954 char *oldpath, *newpath;
955 struct imsgbuf *ibuf;
956 int imsg_fds[2] = {-1, -1};
957 int overlapcnt, done = 0, failed = 0;
960 ibuf = calloc(1, sizeof(*ibuf));
962 err = got_error_from_errno("calloc");
966 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
967 err = got_error_from_errno("socketpair");
973 err = got_error_from_errno("fork");
975 } else if (pid == 0) {
976 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
981 if (close(imsg_fds[1]) == -1) {
982 err = got_error_from_errno("close");
986 imsg_init(ibuf, imsg_fds[0]);
988 err = send_patch(ibuf, fd);
993 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
998 while (!done && err == NULL) {
1000 struct patch_args pa;
1002 pa.progress_cb = progress_cb;
1003 pa.progress_arg = progress_arg;
1006 err = recv_patch(ibuf, &done, &p, strip);
1013 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1014 &newpath, worktree, repo, fileindex);
1016 err = apply_patch(&overlapcnt, worktree, repo,
1017 fileindex, oldpath, newpath, &p, nop, &pa,
1018 cancel_cb, cancel_arg);
1021 /* recoverable errors */
1022 if (err->code == GOT_ERR_FILE_STATUS ||
1023 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1024 err = report_progress(&pa, p.old, p.new,
1025 GOT_STATUS_CANNOT_UPDATE, err);
1026 else if (err->code == GOT_ERR_HUNK_FAILED)
1027 err = report_progress(&pa, p.old, p.new,
1028 GOT_STATUS_CANNOT_UPDATE, NULL);
1030 if (overlapcnt != 0)
1042 if (fileindex != NULL)
1043 complete_err = got_worktree_patch_complete(fileindex,
1045 if (complete_err && err == NULL)
1047 free(fileindex_path);
1048 if (fd != -1 && close(fd) == -1 && err == NULL)
1049 err = got_error_from_errno("close");
1052 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1053 err = got_error_from_errno("close");
1054 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1055 err = got_error_from_errno("close");
1056 if (err == NULL && failed)
1057 err = got_error(GOT_ERR_PATCH_FAILED);