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"
49 #include "got_lib_delta.h"
50 #include "got_lib_diff.h"
51 #include "got_lib_object.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_sha1.h"
55 #define MIN(a, b) ((a) < (b) ? (a) : (b))
57 struct got_patch_hunk {
58 STAILQ_ENTRY(got_patch_hunk) entries;
59 const struct got_error *err;
72 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
78 struct got_patch_hunk_head head;
82 got_patch_progress_cb progress_cb;
84 struct got_patch_hunk_head *head;
87 static const struct got_error *
88 send_patch(struct imsgbuf *ibuf, int fd)
90 const struct got_error *err = NULL;
92 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
94 err = got_error_from_errno(
95 "imsg_compose GOT_IMSG_PATCH_FILE");
100 if (imsg_flush(ibuf) == -1) {
101 err = got_error_from_errno("imsg_flush");
109 patch_free(struct got_patch *p)
111 struct got_patch_hunk *h;
114 while (!STAILQ_EMPTY(&p->head)) {
115 h = STAILQ_FIRST(&p->head);
116 STAILQ_REMOVE_HEAD(&p->head, entries);
118 for (i = 0; i < h->len; ++i)
127 memset(p, 0, sizeof(*p));
128 STAILQ_INIT(&p->head);
131 static const struct got_error *
132 pushline(struct got_patch_hunk *h, const char *line)
137 if (h->len == h->cap) {
138 if ((newcap = h->cap * 1.5) == 0)
140 t = recallocarray(h->lines, h->cap, newcap,
141 sizeof(h->lines[0]));
143 return got_error_from_errno("recallocarray");
148 if ((t = strdup(line)) == NULL)
149 return got_error_from_errno("strdup");
151 h->lines[h->len++] = t;
155 static const struct got_error *
156 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
158 const struct got_error *err = NULL;
160 struct got_imsg_patch_hunk hdr;
161 struct got_imsg_patch patch;
162 struct got_patch_hunk *h = NULL;
166 memset(p, 0, sizeof(*p));
167 STAILQ_INIT(&p->head);
169 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
172 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
176 if (imsg.hdr.type != GOT_IMSG_PATCH) {
177 err = got_error(GOT_ERR_PRIVSEP_MSG);
180 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
181 if (datalen != sizeof(patch)) {
182 err = got_error(GOT_ERR_PRIVSEP_LEN);
185 memcpy(&patch, imsg.data, sizeof(patch));
187 if (patch.old[sizeof(patch.old)-1] != '\0' ||
188 patch.new[sizeof(patch.new)-1] != '\0' ||
189 patch.cid[sizeof(patch.cid)-1] != '\0' ||
190 patch.blob[sizeof(patch.blob)-1] != '\0') {
191 err = got_error(GOT_ERR_PRIVSEP_LEN);
195 if (*patch.cid != '\0')
196 strlcpy(p->cid, patch.cid, sizeof(p->cid));
198 if (*patch.blob != '\0')
199 strlcpy(p->blob, patch.blob, sizeof(p->blob));
201 /* automatically set strip=1 for git-style diffs */
202 if (strip == -1 && patch.git &&
203 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
204 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
207 /* prefer the new name if not /dev/null for not git-style diffs */
208 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
209 err = got_path_strip(&p->old, patch.new, strip);
212 } else if (*patch.old != '\0') {
213 err = got_path_strip(&p->old, patch.old, strip);
218 if (*patch.new != '\0') {
219 err = got_path_strip(&p->new, patch.new, strip);
224 if (p->old == NULL && p->new == NULL) {
225 err = got_error(GOT_ERR_PATCH_MALFORMED);
234 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
240 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
241 switch (imsg.hdr.type) {
242 case GOT_IMSG_PATCH_DONE:
243 if (h != NULL && h->len == 0)
244 err = got_error(GOT_ERR_PATCH_MALFORMED);
246 case GOT_IMSG_PATCH_HUNK:
248 (h->len == 0 || h->old_nonl || h->new_nonl)) {
249 err = got_error(GOT_ERR_PATCH_MALFORMED);
253 if (datalen != sizeof(hdr)) {
254 err = got_error(GOT_ERR_PRIVSEP_LEN);
257 memcpy(&hdr, imsg.data, sizeof(hdr));
258 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
259 err = got_error(GOT_ERR_PRIVSEP_LEN);
262 if ((h = calloc(1, sizeof(*h))) == NULL) {
263 err = got_error_from_errno("calloc");
266 h->old_from = hdr.oldfrom;
267 h->old_lines = hdr.oldlines;
268 h->new_from = hdr.newfrom;
269 h->new_lines = hdr.newlines;
270 STAILQ_INSERT_TAIL(&p->head, h, entries);
272 case GOT_IMSG_PATCH_LINE:
274 err = got_error(GOT_ERR_PRIVSEP_MSG);
278 /* at least one char */
279 if (datalen < 2 || t[datalen-1] != '\0') {
280 err = got_error(GOT_ERR_PRIVSEP_MSG);
283 if (*t != ' ' && *t != '-' && *t != '+' &&
285 err = got_error(GOT_ERR_PRIVSEP_MSG);
290 err = pushline(h, t);
291 else if (lastmode == '-')
293 else if (lastmode == '+')
296 err = got_error(GOT_ERR_PATCH_MALFORMED);
304 err = got_error(GOT_ERR_PRIVSEP_MSG);
320 * Copy data from orig starting at copypos until pos into tmp.
321 * If pos is -1, copy until EOF.
323 static const struct got_error *
324 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
329 if (fseeko(orig, copypos, SEEK_SET) == -1)
330 return got_error_from_errno("fseeko");
332 while (pos == -1 || copypos < pos) {
335 len = MIN(len, (size_t)pos - copypos);
336 r = fread(buf, 1, len, orig);
337 if (r != len && ferror(orig))
338 return got_error_from_errno("fread");
339 w = fwrite(buf, 1, r, tmp);
341 return got_error_from_errno("fwrite");
343 if (r != len && feof(orig)) {
346 return got_error(GOT_ERR_HUNK_FAILED);
352 static const struct got_error *
353 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
355 const struct got_error *err = NULL;
357 char mode = *h->lines[0];
361 int match_lineno = -1;
364 linelen = getline(&line, &linesize, orig);
367 err = got_error_from_errno("getline");
368 else if (match == -1)
369 err = got_error(GOT_ERR_HUNK_FAILED);
372 if (line[linelen - 1] == '\n')
373 line[linelen - 1] = '\0';
376 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
377 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
378 (mode == '+' && *lineno == h->old_from)) {
379 match = ftello(orig);
381 err = got_error_from_errno("ftello");
385 match_lineno = (*lineno)-1;
388 if (*lineno >= h->old_from && match != -1)
394 *lineno = match_lineno;
395 if (fseeko(orig, match, SEEK_SET) == -1)
396 err = got_error_from_errno("fseeko");
403 static const struct got_error *
404 test_hunk(FILE *orig, struct got_patch_hunk *h)
406 const struct got_error *err = NULL;
408 size_t linesize = 0, i = 0;
411 for (i = 0; i < h->len; ++i) {
412 switch (*h->lines[i]) {
417 linelen = getline(&line, &linesize, orig);
420 err = got_error_from_errno("getline");
423 GOT_ERR_HUNK_FAILED);
426 if (line[linelen - 1] == '\n')
427 line[linelen - 1] = '\0';
428 if (strcmp(h->lines[i] + 1, line)) {
429 err = got_error(GOT_ERR_HUNK_FAILED);
441 static const struct got_error *
442 apply_hunk(FILE *tmp, struct got_patch_hunk *h, int *lineno)
446 for (i = 0; i < h->len; ++i) {
447 switch (*h->lines[i]) {
449 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
450 return got_error_from_errno("fprintf");
457 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
458 return got_error_from_errno("fprintf");
459 if (new != h->new_lines || !h->new_nonl) {
460 if (fprintf(tmp, "\n") < 0)
461 return got_error_from_errno(
470 static const struct got_error *
471 patch_file(struct got_patch *p, FILE *orig, FILE *tmp, mode_t *mode)
473 const struct got_error *err = NULL;
474 struct got_patch_hunk *h;
482 if (p->old == NULL) { /* create */
483 h = STAILQ_FIRST(&p->head);
484 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
485 return got_error(GOT_ERR_PATCH_MALFORMED);
486 return apply_hunk(tmp, h, &lineno);
489 if (fstat(fileno(orig), &sb) == -1)
490 return got_error_from_errno("fstat");
494 STAILQ_FOREACH(h, &p->head, entries) {
496 err = locate_hunk(orig, h, &pos, &lineno);
497 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
501 err = copy(tmp, orig, copypos, pos);
506 err = test_hunk(orig, h);
507 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
509 * try to apply the hunk again starting the search
510 * after the previous partial match.
512 if (fseeko(orig, pos, SEEK_SET) == -1)
513 return got_error_from_errno("fseeko");
514 linelen = getline(&line, &linesize, orig);
516 return got_error_from_errno("getline");
523 if (lineno + 1 != h->old_from)
524 h->offset = lineno + 1 - h->old_from;
526 err = apply_hunk(tmp, h, &lineno);
530 copypos = ftello(orig);
532 return got_error_from_errno("ftello");
535 if (p->new == NULL && sb.st_size != copypos) {
536 h = STAILQ_FIRST(&p->head);
537 h->err = got_error(GOT_ERR_HUNK_FAILED);
539 } else if (!feof(orig))
540 err = copy(tmp, orig, copypos, -1);
545 static const struct got_error *
546 report_progress(struct patch_args *pa, const char *old, const char *new,
547 unsigned char status, const struct got_error *orig_error)
549 const struct got_error *err;
550 struct got_patch_hunk *h;
552 err = pa->progress_cb(pa->progress_arg, old, new, status,
553 orig_error, 0, 0, 0, 0, 0, NULL);
557 STAILQ_FOREACH(h, pa->head, entries) {
558 if (h->offset == 0 && h->err == NULL)
561 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
562 h->old_from, h->old_lines, h->new_from, h->new_lines,
571 static const struct got_error *
572 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
575 return report_progress(arg, path, NULL, status, NULL);
578 static const struct got_error *
579 patch_add(void *arg, unsigned char status, const char *path)
581 return report_progress(arg, NULL, path, status, NULL);
584 static const struct got_error *
585 open_blob(char **path, FILE **fp, const char *blobid,
586 struct got_repository *repo)
588 const struct got_error *err = NULL;
589 struct got_blob_object *blob = NULL;
590 struct got_object_id id;
595 if (!got_parse_sha1_digest(id.sha1, blobid))
596 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
598 err = got_object_open_as_blob(&blob, repo, &id, 8192);
602 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
606 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
612 got_object_blob_close(blob);
625 static const struct got_error *
626 apply_patch(int *overlapcnt, struct got_worktree *worktree,
627 struct got_repository *repo, struct got_fileindex *fileindex,
628 const char *old, const char *new, struct got_patch *p, int nop,
629 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
631 const struct got_error *err = NULL;
632 int do_merge = 0, file_renamed = 0;
633 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
634 char *oldpath = NULL, *newpath = NULL;
635 char *tmppath = NULL, *template = NULL, *parent = NULL;
636 char *apath = NULL, *mergepath = NULL;
637 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
640 mode_t mode = GOT_DEFAULT_FILE_MODE;
644 /* don't run the diff3 merge on creations/deletions */
645 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
646 err = open_blob(&apath, &afile, p->blob, repo);
648 * ignore failures to open this blob, we might have
651 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
653 else if (err == NULL)
658 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
660 err = got_error_from_errno("asprintf");
664 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
666 err = got_error_from_errno("asprintf");
670 file_renamed = strcmp(oldpath, newpath);
672 if (asprintf(&template, "%s/got-patch",
673 got_worktree_get_root_path(worktree)) == -1) {
674 err = got_error_from_errno(template);
678 if (p->old != NULL && (oldfile = fopen(oldpath, "r")) == NULL) {
679 err = got_error_from_errno2("open", oldpath);
683 err = got_opentemp_named(&tmppath, &tmpfile, template);
687 outfd = fileno(tmpfile);
688 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile, &mode);
693 const char *type, *id;
695 if (fseeko(afile, 0, SEEK_SET) == -1 ||
696 fseeko(oldfile, 0, SEEK_SET) == -1 ||
697 fseeko(tmpfile, 0, SEEK_SET) == -1) {
698 err = got_error_from_errno("fseeko");
702 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
703 err = got_error_from_errno("asprintf");
708 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
709 err = got_error_from_errno("asprintf");
714 if (*p->cid != '\0') {
722 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
723 err = got_error_from_errno("asprintf");
728 err = got_opentemp_named(&mergepath, &mergefile, template);
732 outfd = fileno(mergefile);
734 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
735 oldfile, oldlabel, anclabel, newlabel,
736 GOT_DIFF_ALGORITHM_PATIENCE);
744 if (p->old != NULL && p->new == NULL) {
745 err = got_worktree_patch_schedule_rm(old, repo, worktree,
746 fileindex, patch_delete, pa);
750 if (fchmod(outfd, mode) == -1) {
751 err = got_error_from_errno2("chmod", tmppath);
755 if (rename(outpath, newpath) == -1) {
756 if (errno != ENOENT) {
757 err = got_error_from_errno3("rename", outpath,
762 err = got_path_dirname(&parent, newpath);
765 err = got_path_mkdir(parent);
768 if (rename(outpath, newpath) == -1) {
769 err = got_error_from_errno3("rename", outpath,
776 err = got_worktree_patch_schedule_rm(old, repo, worktree,
777 fileindex, patch_delete, pa);
779 err = got_worktree_patch_schedule_add(new, repo,
780 worktree, fileindex, patch_add,
784 } else if (p->old == NULL) {
785 err = got_worktree_patch_schedule_add(new, repo, worktree,
786 fileindex, patch_add, pa);
789 } else if (*overlapcnt != 0)
790 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
792 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
794 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
802 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
803 err = got_error_from_errno("fclose");
807 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
808 err = got_error_from_errno("fclose");
812 if (afile != NULL && fclose(afile) == EOF && err == NULL)
813 err = got_error_from_errno("fclose");
816 if (mergepath != NULL)
818 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
819 err = got_error_from_errno("fclose");
830 reverse_patch(struct got_patch *p)
832 struct got_patch_hunk *h;
836 STAILQ_FOREACH(h, &p->head, entries) {
838 h->old_from = h->new_from;
842 h->old_lines = h->new_lines;
846 h->old_nonl = h->new_nonl;
849 for (i = 0; i < h->len; ++i) {
850 if (*h->lines[i] == '+')
852 else if (*h->lines[i] == '-')
858 const struct got_error *
859 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
860 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
861 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
863 const struct got_error *err = NULL, *complete_err = NULL;
864 struct got_fileindex *fileindex = NULL;
865 char *fileindex_path = NULL;
866 char *oldpath, *newpath;
867 struct imsgbuf *ibuf;
868 int imsg_fds[2] = {-1, -1};
869 int overlapcnt, done = 0, failed = 0;
872 ibuf = calloc(1, sizeof(*ibuf));
874 err = got_error_from_errno("calloc");
878 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
879 err = got_error_from_errno("socketpair");
885 err = got_error_from_errno("fork");
887 } else if (pid == 0) {
888 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
893 if (close(imsg_fds[1]) == -1) {
894 err = got_error_from_errno("close");
898 imsg_init(ibuf, imsg_fds[0]);
900 err = send_patch(ibuf, fd);
905 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
910 while (!done && err == NULL) {
912 struct patch_args pa;
914 pa.progress_cb = progress_cb;
915 pa.progress_arg = progress_arg;
918 err = recv_patch(ibuf, &done, &p, strip);
925 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
926 &newpath, worktree, repo, fileindex);
928 err = apply_patch(&overlapcnt, worktree, repo,
929 fileindex, oldpath, newpath, &p, nop, &pa,
930 cancel_cb, cancel_arg);
933 /* recoverable errors */
934 if (err->code == GOT_ERR_FILE_STATUS ||
935 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
936 err = report_progress(&pa, p.old, p.new,
937 GOT_STATUS_CANNOT_UPDATE, err);
938 else if (err->code == GOT_ERR_HUNK_FAILED)
939 err = report_progress(&pa, p.old, p.new,
940 GOT_STATUS_CANNOT_UPDATE, NULL);
954 if (fileindex != NULL)
955 complete_err = got_worktree_patch_complete(fileindex,
957 if (complete_err && err == NULL)
959 free(fileindex_path);
960 if (fd != -1 && close(fd) == -1 && err == NULL)
961 err = got_error_from_errno("close");
964 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
965 err = got_error_from_errno("close");
966 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
967 err = got_error_from_errno("close");
968 if (err == NULL && failed)
969 err = got_error(GOT_ERR_PATCH_FAILED);