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/socket.h>
36 #include "got_error.h"
37 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_cancel.h"
41 #include "got_worktree.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
44 #include "got_patch.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_diff.h"
48 #include "got_lib_object.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_sha1.h"
52 #define MIN(a, b) ((a) < (b) ? (a) : (b))
54 struct got_patch_hunk {
55 STAILQ_ENTRY(got_patch_hunk) entries;
56 const struct got_error *err;
69 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
75 struct got_patch_hunk_head head;
79 got_patch_progress_cb progress_cb;
81 struct got_patch_hunk_head *head;
84 static const struct got_error *
85 send_patch(struct imsgbuf *ibuf, int fd)
87 const struct got_error *err = NULL;
89 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
91 err = got_error_from_errno(
92 "imsg_compose GOT_IMSG_PATCH_FILE");
97 if (imsg_flush(ibuf) == -1) {
98 err = got_error_from_errno("imsg_flush");
106 patch_free(struct got_patch *p)
108 struct got_patch_hunk *h;
111 while (!STAILQ_EMPTY(&p->head)) {
112 h = STAILQ_FIRST(&p->head);
113 STAILQ_REMOVE_HEAD(&p->head, entries);
115 for (i = 0; i < h->len; ++i)
124 memset(p, 0, sizeof(*p));
125 STAILQ_INIT(&p->head);
128 static const struct got_error *
129 pushline(struct got_patch_hunk *h, const char *line)
134 if (h->len == h->cap) {
135 if ((newcap = h->cap * 1.5) == 0)
137 t = recallocarray(h->lines, h->cap, newcap,
138 sizeof(h->lines[0]));
140 return got_error_from_errno("recallocarray");
145 if ((t = strdup(line)) == NULL)
146 return got_error_from_errno("strdup");
148 h->lines[h->len++] = t;
152 static const struct got_error *
153 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
155 const struct got_error *err = NULL;
157 struct got_imsg_patch_hunk hdr;
158 struct got_imsg_patch patch;
159 struct got_patch_hunk *h = NULL;
163 memset(p, 0, sizeof(*p));
164 STAILQ_INIT(&p->head);
166 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
169 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
173 if (imsg.hdr.type != GOT_IMSG_PATCH) {
174 err = got_error(GOT_ERR_PRIVSEP_MSG);
177 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
178 if (datalen != sizeof(patch)) {
179 err = got_error(GOT_ERR_PRIVSEP_LEN);
182 memcpy(&patch, imsg.data, sizeof(patch));
184 if (patch.old[sizeof(patch.old)-1] != '\0' ||
185 patch.new[sizeof(patch.new)-1] != '\0' ||
186 patch.cid[sizeof(patch.cid)-1] != '\0' ||
187 patch.blob[sizeof(patch.blob)-1] != '\0') {
188 err = got_error(GOT_ERR_PRIVSEP_LEN);
192 if (*patch.cid != '\0')
193 strlcpy(p->cid, patch.cid, sizeof(p->cid));
195 if (*patch.blob != '\0')
196 strlcpy(p->blob, patch.blob, sizeof(p->blob));
198 /* automatically set strip=1 for git-style diffs */
199 if (strip == -1 && patch.git &&
200 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
201 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
204 /* prefer the new name if not /dev/null for not git-style diffs */
205 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
206 err = got_path_strip(&p->old, patch.new, strip);
209 } else if (*patch.old != '\0') {
210 err = got_path_strip(&p->old, patch.old, strip);
215 if (*patch.new != '\0') {
216 err = got_path_strip(&p->new, patch.new, strip);
221 if (p->old == NULL && p->new == NULL) {
222 err = got_error(GOT_ERR_PATCH_MALFORMED);
231 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
237 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
238 switch (imsg.hdr.type) {
239 case GOT_IMSG_PATCH_DONE:
240 if (h != NULL && h->len == 0)
241 err = got_error(GOT_ERR_PATCH_MALFORMED);
243 case GOT_IMSG_PATCH_HUNK:
245 (h->len == 0 || h->old_nonl || h->new_nonl)) {
246 err = got_error(GOT_ERR_PATCH_MALFORMED);
250 if (datalen != sizeof(hdr)) {
251 err = got_error(GOT_ERR_PRIVSEP_LEN);
254 memcpy(&hdr, imsg.data, sizeof(hdr));
255 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
256 err = got_error(GOT_ERR_PRIVSEP_LEN);
259 if ((h = calloc(1, sizeof(*h))) == NULL) {
260 err = got_error_from_errno("calloc");
263 h->old_from = hdr.oldfrom;
264 h->old_lines = hdr.oldlines;
265 h->new_from = hdr.newfrom;
266 h->new_lines = hdr.newlines;
267 STAILQ_INSERT_TAIL(&p->head, h, entries);
269 case GOT_IMSG_PATCH_LINE:
271 err = got_error(GOT_ERR_PRIVSEP_MSG);
275 /* at least one char */
276 if (datalen < 2 || t[datalen-1] != '\0') {
277 err = got_error(GOT_ERR_PRIVSEP_MSG);
280 if (*t != ' ' && *t != '-' && *t != '+' &&
282 err = got_error(GOT_ERR_PRIVSEP_MSG);
287 err = pushline(h, t);
288 else if (lastmode == '-')
290 else if (lastmode == '+')
293 err = got_error(GOT_ERR_PATCH_MALFORMED);
301 err = got_error(GOT_ERR_PRIVSEP_MSG);
317 * Copy data from orig starting at copypos until pos into tmp.
318 * If pos is -1, copy until EOF.
320 static const struct got_error *
321 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
326 if (fseeko(orig, copypos, SEEK_SET) == -1)
327 return got_error_from_errno("fseeko");
329 while (pos == -1 || copypos < pos) {
332 len = MIN(len, (size_t)pos - copypos);
333 r = fread(buf, 1, len, orig);
334 if (r != len && ferror(orig))
335 return got_error_from_errno("fread");
336 w = fwrite(buf, 1, r, tmp);
338 return got_error_from_errno("fwrite");
340 if (r != len && feof(orig)) {
343 return got_error(GOT_ERR_HUNK_FAILED);
349 static const struct got_error *
350 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
352 const struct got_error *err = NULL;
354 char mode = *h->lines[0];
358 int match_lineno = -1;
361 linelen = getline(&line, &linesize, orig);
364 err = got_error_from_errno("getline");
365 else if (match == -1)
366 err = got_error(GOT_ERR_HUNK_FAILED);
369 if (line[linelen - 1] == '\n')
370 line[linelen - 1] = '\0';
373 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
374 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
375 (mode == '+' && *lineno == h->old_from)) {
376 match = ftello(orig);
378 err = got_error_from_errno("ftello");
382 match_lineno = (*lineno)-1;
385 if (*lineno >= h->old_from && match != -1)
391 *lineno = match_lineno;
392 if (fseeko(orig, match, SEEK_SET) == -1)
393 err = got_error_from_errno("fseeko");
400 static const struct got_error *
401 test_hunk(FILE *orig, struct got_patch_hunk *h)
403 const struct got_error *err = NULL;
405 size_t linesize = 0, i = 0;
408 for (i = 0; i < h->len; ++i) {
409 switch (*h->lines[i]) {
414 linelen = getline(&line, &linesize, orig);
417 err = got_error_from_errno("getline");
420 GOT_ERR_HUNK_FAILED);
423 if (line[linelen - 1] == '\n')
424 line[linelen - 1] = '\0';
425 if (strcmp(h->lines[i] + 1, line)) {
426 err = got_error(GOT_ERR_HUNK_FAILED);
438 static const struct got_error *
439 apply_hunk(FILE *tmp, struct got_patch_hunk *h, int *lineno)
443 for (i = 0; i < h->len; ++i) {
444 switch (*h->lines[i]) {
446 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
447 return got_error_from_errno("fprintf");
454 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
455 return got_error_from_errno("fprintf");
456 if (new != h->new_lines || !h->new_nonl) {
457 if (fprintf(tmp, "\n") < 0)
458 return got_error_from_errno(
467 static const struct got_error *
468 patch_file(struct got_patch *p, FILE *orig, FILE *tmp, mode_t *mode)
470 const struct got_error *err = NULL;
471 struct got_patch_hunk *h;
479 if (p->old == NULL) { /* create */
480 h = STAILQ_FIRST(&p->head);
481 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
482 return got_error(GOT_ERR_PATCH_MALFORMED);
483 return apply_hunk(tmp, h, &lineno);
486 if (fstat(fileno(orig), &sb) == -1)
487 return got_error_from_errno("fstat");
491 STAILQ_FOREACH(h, &p->head, entries) {
493 err = locate_hunk(orig, h, &pos, &lineno);
494 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
498 err = copy(tmp, orig, copypos, pos);
503 err = test_hunk(orig, h);
504 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
506 * try to apply the hunk again starting the search
507 * after the previous partial match.
509 if (fseeko(orig, pos, SEEK_SET) == -1)
510 return got_error_from_errno("fseeko");
511 linelen = getline(&line, &linesize, orig);
513 return got_error_from_errno("getline");
520 if (lineno + 1 != h->old_from)
521 h->offset = lineno + 1 - h->old_from;
523 err = apply_hunk(tmp, h, &lineno);
527 copypos = ftello(orig);
529 return got_error_from_errno("ftello");
532 if (p->new == NULL && sb.st_size != copypos) {
533 h = STAILQ_FIRST(&p->head);
534 h->err = got_error(GOT_ERR_HUNK_FAILED);
536 } else if (!feof(orig))
537 err = copy(tmp, orig, copypos, -1);
542 static const struct got_error *
543 report_progress(struct patch_args *pa, const char *old, const char *new,
544 unsigned char status, const struct got_error *orig_error)
546 const struct got_error *err;
547 struct got_patch_hunk *h;
549 err = pa->progress_cb(pa->progress_arg, old, new, status,
550 orig_error, 0, 0, 0, 0, 0, NULL);
554 STAILQ_FOREACH(h, pa->head, entries) {
555 if (h->offset == 0 && h->err == NULL)
558 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
559 h->old_from, h->old_lines, h->new_from, h->new_lines,
568 static const struct got_error *
569 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
572 return report_progress(arg, path, NULL, status, NULL);
575 static const struct got_error *
576 patch_add(void *arg, unsigned char status, const char *path)
578 return report_progress(arg, NULL, path, status, NULL);
581 static const struct got_error *
582 open_blob(char **path, FILE **fp, const char *blobid,
583 struct got_repository *repo)
585 const struct got_error *err = NULL;
586 struct got_blob_object *blob = NULL;
587 struct got_object_id id, *idptr, *matched_id = NULL;
592 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
593 err = got_repo_match_object_id(&matched_id, NULL, blobid,
594 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
600 if (!got_parse_sha1_digest(id.sha1, blobid))
601 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
605 err = got_object_open_as_blob(&blob, repo, idptr, 8192);
609 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
613 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
619 got_object_blob_close(blob);
620 if (matched_id != NULL)
634 static const struct got_error *
635 apply_patch(int *overlapcnt, struct got_worktree *worktree,
636 struct got_repository *repo, struct got_fileindex *fileindex,
637 const char *old, const char *new, struct got_patch *p, int nop,
638 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
640 const struct got_error *err = NULL;
641 int do_merge = 0, file_renamed = 0;
642 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
643 char *oldpath = NULL, *newpath = NULL;
644 char *tmppath = NULL, *template = NULL, *parent = NULL;
645 char *apath = NULL, *mergepath = NULL;
646 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
649 mode_t mode = GOT_DEFAULT_FILE_MODE;
653 /* don't run the diff3 merge on creations/deletions */
654 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
655 err = open_blob(&apath, &afile, p->blob, repo);
657 * ignore failures to open this blob, we might have
660 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
661 err->code != GOT_ERR_NO_OBJ)
663 else if (err == NULL)
668 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
670 err = got_error_from_errno("asprintf");
674 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
676 err = got_error_from_errno("asprintf");
680 file_renamed = strcmp(oldpath, newpath);
682 if (asprintf(&template, "%s/got-patch",
683 got_worktree_get_root_path(worktree)) == -1) {
684 err = got_error_from_errno(template);
688 if (p->old != NULL && (oldfile = fopen(oldpath, "r")) == NULL) {
689 err = got_error_from_errno2("open", oldpath);
693 err = got_opentemp_named(&tmppath, &tmpfile, template);
697 outfd = fileno(tmpfile);
698 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile, &mode);
703 const char *type, *id;
705 if (fseeko(afile, 0, SEEK_SET) == -1 ||
706 fseeko(oldfile, 0, SEEK_SET) == -1 ||
707 fseeko(tmpfile, 0, SEEK_SET) == -1) {
708 err = got_error_from_errno("fseeko");
712 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
713 err = got_error_from_errno("asprintf");
718 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
719 err = got_error_from_errno("asprintf");
724 if (*p->cid != '\0') {
732 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
733 err = got_error_from_errno("asprintf");
738 err = got_opentemp_named(&mergepath, &mergefile, template);
742 outfd = fileno(mergefile);
744 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
745 oldfile, oldlabel, anclabel, newlabel,
746 GOT_DIFF_ALGORITHM_PATIENCE);
754 if (p->old != NULL && p->new == NULL) {
755 err = got_worktree_patch_schedule_rm(old, repo, worktree,
756 fileindex, patch_delete, pa);
760 if (fchmod(outfd, mode) == -1) {
761 err = got_error_from_errno2("chmod", tmppath);
765 if (rename(outpath, newpath) == -1) {
766 if (errno != ENOENT) {
767 err = got_error_from_errno3("rename", outpath,
772 err = got_path_dirname(&parent, newpath);
775 err = got_path_mkdir(parent);
778 if (rename(outpath, newpath) == -1) {
779 err = got_error_from_errno3("rename", outpath,
786 err = got_worktree_patch_schedule_rm(old, repo, worktree,
787 fileindex, patch_delete, pa);
789 err = got_worktree_patch_schedule_add(new, repo,
790 worktree, fileindex, patch_add,
794 } else if (p->old == NULL) {
795 err = got_worktree_patch_schedule_add(new, repo, worktree,
796 fileindex, patch_add, pa);
799 } else if (*overlapcnt != 0)
800 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
802 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
804 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
812 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
813 err = got_error_from_errno("fclose");
817 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
818 err = got_error_from_errno("fclose");
822 if (afile != NULL && fclose(afile) == EOF && err == NULL)
823 err = got_error_from_errno("fclose");
826 if (mergepath != NULL)
828 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
829 err = got_error_from_errno("fclose");
840 reverse_patch(struct got_patch *p)
842 struct got_patch_hunk *h;
846 STAILQ_FOREACH(h, &p->head, entries) {
848 h->old_from = h->new_from;
852 h->old_lines = h->new_lines;
856 h->old_nonl = h->new_nonl;
859 for (i = 0; i < h->len; ++i) {
860 if (*h->lines[i] == '+')
862 else if (*h->lines[i] == '-')
868 const struct got_error *
869 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
870 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
871 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
873 const struct got_error *err = NULL, *complete_err = NULL;
874 struct got_fileindex *fileindex = NULL;
875 char *fileindex_path = NULL;
876 char *oldpath, *newpath;
877 struct imsgbuf *ibuf;
878 int imsg_fds[2] = {-1, -1};
879 int overlapcnt, done = 0, failed = 0;
882 ibuf = calloc(1, sizeof(*ibuf));
884 err = got_error_from_errno("calloc");
888 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
889 err = got_error_from_errno("socketpair");
895 err = got_error_from_errno("fork");
897 } else if (pid == 0) {
898 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
903 if (close(imsg_fds[1]) == -1) {
904 err = got_error_from_errno("close");
908 imsg_init(ibuf, imsg_fds[0]);
910 err = send_patch(ibuf, fd);
915 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
920 while (!done && err == NULL) {
922 struct patch_args pa;
924 pa.progress_cb = progress_cb;
925 pa.progress_arg = progress_arg;
928 err = recv_patch(ibuf, &done, &p, strip);
935 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
936 &newpath, worktree, repo, fileindex);
938 err = apply_patch(&overlapcnt, worktree, repo,
939 fileindex, oldpath, newpath, &p, nop, &pa,
940 cancel_cb, cancel_arg);
943 /* recoverable errors */
944 if (err->code == GOT_ERR_FILE_STATUS ||
945 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
946 err = report_progress(&pa, p.old, p.new,
947 GOT_STATUS_CANNOT_UPDATE, err);
948 else if (err->code == GOT_ERR_HUNK_FAILED)
949 err = report_progress(&pa, p.old, p.new,
950 GOT_STATUS_CANNOT_UPDATE, NULL);
964 if (fileindex != NULL)
965 complete_err = got_worktree_patch_complete(fileindex,
967 if (complete_err && err == NULL)
969 free(fileindex_path);
970 if (fd != -1 && close(fd) == -1 && err == NULL)
971 err = got_error_from_errno("close");
974 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
975 err = got_error_from_errno("close");
976 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
977 err = got_error_from_errno("close");
978 if (err == NULL && failed)
979 err = got_error(GOT_ERR_PATCH_FAILED);