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>
39 #include "got_error.h"
40 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_repository.h"
46 #include "got_opentemp.h"
47 #include "got_patch.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_diff.h"
52 #include "got_lib_object.h"
53 #include "got_lib_privsep.h"
54 #include "got_lib_sha1.h"
56 #define MIN(a, b) ((a) < (b) ? (a) : (b))
58 struct got_patch_hunk {
59 STAILQ_ENTRY(got_patch_hunk) entries;
60 const struct got_error *err;
73 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
79 struct got_patch_hunk_head head;
83 got_patch_progress_cb progress_cb;
85 struct got_patch_hunk_head *head;
88 static const struct got_error *
89 send_patch(struct imsgbuf *ibuf, int fd)
91 const struct got_error *err = NULL;
93 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
95 err = got_error_from_errno(
96 "imsg_compose GOT_IMSG_PATCH_FILE");
101 if (imsg_flush(ibuf) == -1) {
102 err = got_error_from_errno("imsg_flush");
110 patch_free(struct got_patch *p)
112 struct got_patch_hunk *h;
115 while (!STAILQ_EMPTY(&p->head)) {
116 h = STAILQ_FIRST(&p->head);
117 STAILQ_REMOVE_HEAD(&p->head, entries);
119 for (i = 0; i < h->len; ++i)
128 memset(p, 0, sizeof(*p));
129 STAILQ_INIT(&p->head);
132 static const struct got_error *
133 pushline(struct got_patch_hunk *h, const char *line)
138 if (h->len == h->cap) {
139 if ((newcap = h->cap * 1.5) == 0)
141 t = recallocarray(h->lines, h->cap, newcap,
142 sizeof(h->lines[0]));
144 return got_error_from_errno("recallocarray");
149 if ((t = strdup(line)) == NULL)
150 return got_error_from_errno("strdup");
152 h->lines[h->len++] = t;
156 static const struct got_error *
157 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
159 const struct got_error *err = NULL;
161 struct got_imsg_patch_hunk hdr;
162 struct got_imsg_patch patch;
163 struct got_patch_hunk *h = NULL;
167 memset(p, 0, sizeof(*p));
168 STAILQ_INIT(&p->head);
170 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
173 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
177 if (imsg.hdr.type != GOT_IMSG_PATCH) {
178 err = got_error(GOT_ERR_PRIVSEP_MSG);
181 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
182 if (datalen != sizeof(patch)) {
183 err = got_error(GOT_ERR_PRIVSEP_LEN);
186 memcpy(&patch, imsg.data, sizeof(patch));
188 if (patch.old[sizeof(patch.old)-1] != '\0' ||
189 patch.new[sizeof(patch.new)-1] != '\0' ||
190 patch.cid[sizeof(patch.cid)-1] != '\0' ||
191 patch.blob[sizeof(patch.blob)-1] != '\0') {
192 err = got_error(GOT_ERR_PRIVSEP_LEN);
196 if (*patch.cid != '\0')
197 strlcpy(p->cid, patch.cid, sizeof(p->cid));
199 if (*patch.blob != '\0')
200 strlcpy(p->blob, patch.blob, sizeof(p->blob));
202 /* automatically set strip=1 for git-style diffs */
203 if (strip == -1 && patch.git &&
204 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
205 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
208 /* prefer the new name if not /dev/null for not git-style diffs */
209 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
210 err = got_path_strip(&p->old, patch.new, strip);
213 } else if (*patch.old != '\0') {
214 err = got_path_strip(&p->old, patch.old, strip);
219 if (*patch.new != '\0') {
220 err = got_path_strip(&p->new, patch.new, strip);
225 if (p->old == NULL && p->new == NULL) {
226 err = got_error(GOT_ERR_PATCH_MALFORMED);
235 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
241 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
242 switch (imsg.hdr.type) {
243 case GOT_IMSG_PATCH_DONE:
244 if (h != NULL && h->len == 0)
245 err = got_error(GOT_ERR_PATCH_MALFORMED);
247 case GOT_IMSG_PATCH_HUNK:
249 (h->len == 0 || h->old_nonl || h->new_nonl)) {
250 err = got_error(GOT_ERR_PATCH_MALFORMED);
254 if (datalen != sizeof(hdr)) {
255 err = got_error(GOT_ERR_PRIVSEP_LEN);
258 memcpy(&hdr, imsg.data, sizeof(hdr));
259 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
260 err = got_error(GOT_ERR_PRIVSEP_LEN);
263 if ((h = calloc(1, sizeof(*h))) == NULL) {
264 err = got_error_from_errno("calloc");
267 h->old_from = hdr.oldfrom;
268 h->old_lines = hdr.oldlines;
269 h->new_from = hdr.newfrom;
270 h->new_lines = hdr.newlines;
271 STAILQ_INSERT_TAIL(&p->head, h, entries);
273 case GOT_IMSG_PATCH_LINE:
275 err = got_error(GOT_ERR_PRIVSEP_MSG);
279 /* at least one char */
280 if (datalen < 2 || t[datalen-1] != '\0') {
281 err = got_error(GOT_ERR_PRIVSEP_MSG);
284 if (*t != ' ' && *t != '-' && *t != '+' &&
286 err = got_error(GOT_ERR_PRIVSEP_MSG);
291 err = pushline(h, t);
292 else if (lastmode == '-')
294 else if (lastmode == '+')
297 err = got_error(GOT_ERR_PATCH_MALFORMED);
305 err = got_error(GOT_ERR_PRIVSEP_MSG);
321 * Copy data from orig starting at copypos until pos into tmp.
322 * If pos is -1, copy until EOF.
324 static const struct got_error *
325 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
330 if (fseeko(orig, copypos, SEEK_SET) == -1)
331 return got_error_from_errno("fseeko");
333 while (pos == -1 || copypos < pos) {
336 len = MIN(len, (size_t)pos - copypos);
337 r = fread(buf, 1, len, orig);
338 if (r != len && ferror(orig))
339 return got_error_from_errno("fread");
340 w = fwrite(buf, 1, r, tmp);
342 return got_error_from_errno("fwrite");
344 if (r != len && feof(orig)) {
347 return got_error(GOT_ERR_HUNK_FAILED);
353 static const struct got_error *
354 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
356 const struct got_error *err = NULL;
358 char mode = *h->lines[0];
362 int match_lineno = -1;
365 linelen = getline(&line, &linesize, orig);
368 err = got_error_from_errno("getline");
369 else if (match == -1)
370 err = got_error(GOT_ERR_HUNK_FAILED);
373 if (line[linelen - 1] == '\n')
374 line[linelen - 1] = '\0';
377 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
378 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
379 (mode == '+' && *lineno == h->old_from)) {
380 match = ftello(orig);
382 err = got_error_from_errno("ftello");
386 match_lineno = (*lineno)-1;
389 if (*lineno >= h->old_from && match != -1)
395 *lineno = match_lineno;
396 if (fseeko(orig, match, SEEK_SET) == -1)
397 err = got_error_from_errno("fseeko");
404 static const struct got_error *
405 test_hunk(FILE *orig, struct got_patch_hunk *h)
407 const struct got_error *err = NULL;
409 size_t linesize = 0, i = 0;
412 for (i = 0; i < h->len; ++i) {
413 switch (*h->lines[i]) {
418 linelen = getline(&line, &linesize, orig);
421 err = got_error_from_errno("getline");
424 GOT_ERR_HUNK_FAILED);
427 if (line[linelen - 1] == '\n')
428 line[linelen - 1] = '\0';
429 if (strcmp(h->lines[i] + 1, line)) {
430 err = got_error(GOT_ERR_HUNK_FAILED);
442 static const struct got_error *
443 apply_hunk(FILE *tmp, struct got_patch_hunk *h, int *lineno)
447 for (i = 0; i < h->len; ++i) {
448 switch (*h->lines[i]) {
450 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
451 return got_error_from_errno("fprintf");
458 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
459 return got_error_from_errno("fprintf");
460 if (new != h->new_lines || !h->new_nonl) {
461 if (fprintf(tmp, "\n") < 0)
462 return got_error_from_errno(
471 static const struct got_error *
472 patch_file(struct got_patch *p, FILE *orig, FILE *tmp, mode_t *mode)
474 const struct got_error *err = NULL;
475 struct got_patch_hunk *h;
483 if (p->old == NULL) { /* create */
484 h = STAILQ_FIRST(&p->head);
485 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
486 return got_error(GOT_ERR_PATCH_MALFORMED);
487 return apply_hunk(tmp, h, &lineno);
490 if (fstat(fileno(orig), &sb) == -1)
491 return got_error_from_errno("fstat");
495 STAILQ_FOREACH(h, &p->head, entries) {
497 err = locate_hunk(orig, h, &pos, &lineno);
498 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
502 err = copy(tmp, orig, copypos, pos);
507 err = test_hunk(orig, h);
508 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
510 * try to apply the hunk again starting the search
511 * after the previous partial match.
513 if (fseeko(orig, pos, SEEK_SET) == -1)
514 return got_error_from_errno("fseeko");
515 linelen = getline(&line, &linesize, orig);
517 return got_error_from_errno("getline");
524 if (lineno + 1 != h->old_from)
525 h->offset = lineno + 1 - h->old_from;
527 err = apply_hunk(tmp, h, &lineno);
531 copypos = ftello(orig);
533 return got_error_from_errno("ftello");
536 if (p->new == NULL && sb.st_size != copypos) {
537 h = STAILQ_FIRST(&p->head);
538 h->err = got_error(GOT_ERR_HUNK_FAILED);
540 } else if (!feof(orig))
541 err = copy(tmp, orig, copypos, -1);
546 static const struct got_error *
547 report_progress(struct patch_args *pa, const char *old, const char *new,
548 unsigned char status, const struct got_error *orig_error)
550 const struct got_error *err;
551 struct got_patch_hunk *h;
553 err = pa->progress_cb(pa->progress_arg, old, new, status,
554 orig_error, 0, 0, 0, 0, 0, NULL);
558 STAILQ_FOREACH(h, pa->head, entries) {
559 if (h->offset == 0 && h->err == NULL)
562 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
563 h->old_from, h->old_lines, h->new_from, h->new_lines,
572 static const struct got_error *
573 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
576 return report_progress(arg, path, NULL, status, NULL);
579 static const struct got_error *
580 patch_add(void *arg, unsigned char status, const char *path)
582 return report_progress(arg, NULL, path, status, NULL);
585 static const struct got_error *
586 open_blob(char **path, FILE **fp, const char *blobid,
587 struct got_repository *repo)
589 const struct got_error *err = NULL;
590 struct got_blob_object *blob = NULL;
591 struct got_object_id id, *idptr, *matched_id = NULL;
597 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
598 err = got_repo_match_object_id(&matched_id, NULL, blobid,
599 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
605 if (!got_parse_sha1_digest(id.sha1, blobid))
606 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
610 fd = got_opentempfd();
612 err = got_error_from_errno("got_opentempfd");
616 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
620 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
624 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
629 if (fd != -1 && close(fd) == -1 && err == NULL)
630 err = got_error_from_errno("close");
632 got_object_blob_close(blob);
633 if (matched_id != NULL)
647 static const struct got_error *
648 apply_patch(int *overlapcnt, struct got_worktree *worktree,
649 struct got_repository *repo, struct got_fileindex *fileindex,
650 const char *old, const char *new, struct got_patch *p, int nop,
651 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
653 const struct got_error *err = NULL;
654 int do_merge = 0, file_renamed = 0;
655 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
656 char *oldpath = NULL, *newpath = NULL;
657 char *tmppath = NULL, *template = NULL, *parent = NULL;
658 char *apath = NULL, *mergepath = NULL;
659 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
662 mode_t mode = GOT_DEFAULT_FILE_MODE;
666 /* don't run the diff3 merge on creations/deletions */
667 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
668 err = open_blob(&apath, &afile, p->blob, repo);
670 * ignore failures to open this blob, we might have
673 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
674 err->code != GOT_ERR_NO_OBJ)
676 else if (err == NULL)
681 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
683 err = got_error_from_errno("asprintf");
687 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
689 err = got_error_from_errno("asprintf");
693 file_renamed = strcmp(oldpath, newpath);
695 if (asprintf(&template, "%s/got-patch",
696 got_worktree_get_root_path(worktree)) == -1) {
697 err = got_error_from_errno(template);
701 if (p->old != NULL && (oldfile = fopen(oldpath, "r")) == NULL) {
702 err = got_error_from_errno2("open", oldpath);
706 err = got_opentemp_named(&tmppath, &tmpfile, template);
710 outfd = fileno(tmpfile);
711 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile, &mode);
716 const char *type, *id;
718 if (fseeko(afile, 0, SEEK_SET) == -1 ||
719 fseeko(oldfile, 0, SEEK_SET) == -1 ||
720 fseeko(tmpfile, 0, SEEK_SET) == -1) {
721 err = got_error_from_errno("fseeko");
725 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
726 err = got_error_from_errno("asprintf");
731 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
732 err = got_error_from_errno("asprintf");
737 if (*p->cid != '\0') {
745 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
746 err = got_error_from_errno("asprintf");
751 err = got_opentemp_named(&mergepath, &mergefile, template);
755 outfd = fileno(mergefile);
757 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
758 oldfile, oldlabel, anclabel, newlabel,
759 GOT_DIFF_ALGORITHM_PATIENCE);
767 if (p->old != NULL && p->new == NULL) {
768 err = got_worktree_patch_schedule_rm(old, repo, worktree,
769 fileindex, patch_delete, pa);
773 if (fchmod(outfd, mode) == -1) {
774 err = got_error_from_errno2("chmod", tmppath);
778 if (rename(outpath, newpath) == -1) {
779 if (errno != ENOENT) {
780 err = got_error_from_errno3("rename", outpath,
785 err = got_path_dirname(&parent, newpath);
788 err = got_path_mkdir(parent);
791 if (rename(outpath, newpath) == -1) {
792 err = got_error_from_errno3("rename", outpath,
799 err = got_worktree_patch_schedule_rm(old, repo, worktree,
800 fileindex, patch_delete, pa);
802 err = got_worktree_patch_schedule_add(new, repo,
803 worktree, fileindex, patch_add,
807 } else if (p->old == NULL) {
808 err = got_worktree_patch_schedule_add(new, repo, worktree,
809 fileindex, patch_add, pa);
812 } else if (*overlapcnt != 0)
813 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
815 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
817 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
825 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
826 err = got_error_from_errno("fclose");
830 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
831 err = got_error_from_errno("fclose");
835 if (afile != NULL && fclose(afile) == EOF && err == NULL)
836 err = got_error_from_errno("fclose");
839 if (mergepath != NULL)
841 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
842 err = got_error_from_errno("fclose");
853 reverse_patch(struct got_patch *p)
855 struct got_patch_hunk *h;
859 STAILQ_FOREACH(h, &p->head, entries) {
861 h->old_from = h->new_from;
865 h->old_lines = h->new_lines;
869 h->old_nonl = h->new_nonl;
872 for (i = 0; i < h->len; ++i) {
873 if (*h->lines[i] == '+')
875 else if (*h->lines[i] == '-')
881 const struct got_error *
882 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
883 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
884 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
886 const struct got_error *err = NULL, *complete_err = NULL;
887 struct got_fileindex *fileindex = NULL;
888 char *fileindex_path = NULL;
889 char *oldpath, *newpath;
890 struct imsgbuf *ibuf;
891 int imsg_fds[2] = {-1, -1};
892 int overlapcnt, done = 0, failed = 0;
895 ibuf = calloc(1, sizeof(*ibuf));
897 err = got_error_from_errno("calloc");
901 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
902 err = got_error_from_errno("socketpair");
908 err = got_error_from_errno("fork");
910 } else if (pid == 0) {
911 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
916 if (close(imsg_fds[1]) == -1) {
917 err = got_error_from_errno("close");
921 imsg_init(ibuf, imsg_fds[0]);
923 err = send_patch(ibuf, fd);
928 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
933 while (!done && err == NULL) {
935 struct patch_args pa;
937 pa.progress_cb = progress_cb;
938 pa.progress_arg = progress_arg;
941 err = recv_patch(ibuf, &done, &p, strip);
948 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
949 &newpath, worktree, repo, fileindex);
951 err = apply_patch(&overlapcnt, worktree, repo,
952 fileindex, oldpath, newpath, &p, nop, &pa,
953 cancel_cb, cancel_arg);
956 /* recoverable errors */
957 if (err->code == GOT_ERR_FILE_STATUS ||
958 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
959 err = report_progress(&pa, p.old, p.new,
960 GOT_STATUS_CANNOT_UPDATE, err);
961 else if (err->code == GOT_ERR_HUNK_FAILED)
962 err = report_progress(&pa, p.old, p.new,
963 GOT_STATUS_CANNOT_UPDATE, NULL);
977 if (fileindex != NULL)
978 complete_err = got_worktree_patch_complete(fileindex,
980 if (complete_err && err == NULL)
982 free(fileindex_path);
983 if (fd != -1 && close(fd) == -1 && err == NULL)
984 err = got_error_from_errno("close");
987 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
988 err = got_error_from_errno("close");
989 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
990 err = got_error_from_errno("close");
991 if (err == NULL && failed)
992 err = got_error(GOT_ERR_PATCH_FAILED);