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' && *patch.blob != '\0') {
193 strlcpy(p->cid, patch.cid, sizeof(p->cid));
194 strlcpy(p->blob, patch.blob, sizeof(p->blob));
197 /* automatically set strip=1 for git-style diffs */
198 if (strip == -1 && patch.git &&
199 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
200 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
203 /* prefer the new name if not /dev/null for not git-style diffs */
204 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
205 err = got_path_strip(&p->old, patch.new, strip);
208 } else if (*patch.old != '\0') {
209 err = got_path_strip(&p->old, patch.old, strip);
214 if (*patch.new != '\0') {
215 err = got_path_strip(&p->new, patch.new, strip);
220 if (p->old == NULL && p->new == NULL) {
221 err = got_error(GOT_ERR_PATCH_MALFORMED);
230 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
236 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
237 switch (imsg.hdr.type) {
238 case GOT_IMSG_PATCH_DONE:
239 if (h != NULL && h->len == 0)
240 err = got_error(GOT_ERR_PATCH_MALFORMED);
242 case GOT_IMSG_PATCH_HUNK:
244 (h->len == 0 || h->old_nonl || h->new_nonl)) {
245 err = got_error(GOT_ERR_PATCH_MALFORMED);
249 if (datalen != sizeof(hdr)) {
250 err = got_error(GOT_ERR_PRIVSEP_LEN);
253 memcpy(&hdr, imsg.data, sizeof(hdr));
254 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
255 err = got_error(GOT_ERR_PRIVSEP_LEN);
258 if ((h = calloc(1, sizeof(*h))) == NULL) {
259 err = got_error_from_errno("calloc");
262 h->old_from = hdr.oldfrom;
263 h->old_lines = hdr.oldlines;
264 h->new_from = hdr.newfrom;
265 h->new_lines = hdr.newlines;
266 STAILQ_INSERT_TAIL(&p->head, h, entries);
268 case GOT_IMSG_PATCH_LINE:
270 err = got_error(GOT_ERR_PRIVSEP_MSG);
274 /* at least one char */
275 if (datalen < 2 || t[datalen-1] != '\0') {
276 err = got_error(GOT_ERR_PRIVSEP_MSG);
279 if (*t != ' ' && *t != '-' && *t != '+' &&
281 err = got_error(GOT_ERR_PRIVSEP_MSG);
286 err = pushline(h, t);
287 else if (lastmode == '-')
289 else if (lastmode == '+')
292 err = got_error(GOT_ERR_PATCH_MALFORMED);
300 err = got_error(GOT_ERR_PRIVSEP_MSG);
316 * Copy data from orig starting at copypos until pos into tmp.
317 * If pos is -1, copy until EOF.
319 static const struct got_error *
320 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
325 if (fseeko(orig, copypos, SEEK_SET) == -1)
326 return got_error_from_errno("fseeko");
328 while (pos == -1 || copypos < pos) {
331 len = MIN(len, (size_t)pos - copypos);
332 r = fread(buf, 1, len, orig);
333 if (r != len && ferror(orig))
334 return got_error_from_errno("fread");
335 w = fwrite(buf, 1, r, tmp);
337 return got_error_from_errno("fwrite");
339 if (r != len && feof(orig)) {
342 return got_error(GOT_ERR_HUNK_FAILED);
348 static const struct got_error *
349 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
351 const struct got_error *err = NULL;
353 char mode = *h->lines[0];
357 int match_lineno = -1;
360 linelen = getline(&line, &linesize, orig);
363 err = got_error_from_errno("getline");
364 else if (match == -1)
365 err = got_error(GOT_ERR_HUNK_FAILED);
368 if (line[linelen - 1] == '\n')
369 line[linelen - 1] = '\0';
372 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
373 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
374 (mode == '+' && *lineno == h->old_from)) {
375 match = ftello(orig);
377 err = got_error_from_errno("ftello");
381 match_lineno = (*lineno)-1;
384 if (*lineno >= h->old_from && match != -1)
390 *lineno = match_lineno;
391 if (fseeko(orig, match, SEEK_SET) == -1)
392 err = got_error_from_errno("fseeko");
399 static const struct got_error *
400 test_hunk(FILE *orig, struct got_patch_hunk *h)
402 const struct got_error *err = NULL;
404 size_t linesize = 0, i = 0;
407 for (i = 0; i < h->len; ++i) {
408 switch (*h->lines[i]) {
413 linelen = getline(&line, &linesize, orig);
416 err = got_error_from_errno("getline");
419 GOT_ERR_HUNK_FAILED);
422 if (line[linelen - 1] == '\n')
423 line[linelen - 1] = '\0';
424 if (strcmp(h->lines[i] + 1, line)) {
425 err = got_error(GOT_ERR_HUNK_FAILED);
437 static const struct got_error *
438 apply_hunk(FILE *tmp, struct got_patch_hunk *h, int *lineno)
442 for (i = 0; i < h->len; ++i) {
443 switch (*h->lines[i]) {
445 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
446 return got_error_from_errno("fprintf");
453 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
454 return got_error_from_errno("fprintf");
455 if (new != h->new_lines || !h->new_nonl) {
456 if (fprintf(tmp, "\n") < 0)
457 return got_error_from_errno(
466 static const struct got_error *
467 patch_file(struct got_patch *p, FILE *orig, FILE *tmp, mode_t *mode)
469 const struct got_error *err = NULL;
470 struct got_patch_hunk *h;
478 if (p->old == NULL) { /* create */
479 h = STAILQ_FIRST(&p->head);
480 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
481 return got_error(GOT_ERR_PATCH_MALFORMED);
482 return apply_hunk(tmp, h, &lineno);
485 if (fstat(fileno(orig), &sb) == -1)
486 return got_error_from_errno("fstat");
490 STAILQ_FOREACH(h, &p->head, entries) {
492 err = locate_hunk(orig, h, &pos, &lineno);
493 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
497 err = copy(tmp, orig, copypos, pos);
502 err = test_hunk(orig, h);
503 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
505 * try to apply the hunk again starting the search
506 * after the previous partial match.
508 if (fseeko(orig, pos, SEEK_SET) == -1)
509 return got_error_from_errno("fseeko");
510 linelen = getline(&line, &linesize, orig);
512 return got_error_from_errno("getline");
519 if (lineno + 1 != h->old_from)
520 h->offset = lineno + 1 - h->old_from;
522 err = apply_hunk(tmp, h, &lineno);
526 copypos = ftello(orig);
528 return got_error_from_errno("ftello");
531 if (p->new == NULL && sb.st_size != copypos) {
532 h = STAILQ_FIRST(&p->head);
533 h->err = got_error(GOT_ERR_HUNK_FAILED);
535 } else if (!feof(orig))
536 err = copy(tmp, orig, copypos, -1);
541 static const struct got_error *
542 report_progress(struct patch_args *pa, const char *old, const char *new,
543 unsigned char status, const struct got_error *orig_error)
545 const struct got_error *err;
546 struct got_patch_hunk *h;
548 err = pa->progress_cb(pa->progress_arg, old, new, status,
549 orig_error, 0, 0, 0, 0, 0, NULL);
553 STAILQ_FOREACH(h, pa->head, entries) {
554 if (h->offset == 0 && h->err == NULL)
557 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
558 h->old_from, h->old_lines, h->new_from, h->new_lines,
567 static const struct got_error *
568 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
571 return report_progress(arg, path, NULL, status, NULL);
574 static const struct got_error *
575 patch_add(void *arg, unsigned char status, const char *path)
577 return report_progress(arg, NULL, path, status, NULL);
580 static const struct got_error *
581 open_blob(char **path, FILE **fp, const char *blobid,
582 struct got_repository *repo)
584 const struct got_error *err = NULL;
585 struct got_blob_object *blob = NULL;
586 struct got_object_id id;
591 if (!got_parse_sha1_digest(id.sha1, blobid))
592 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
594 err = got_object_open_as_blob(&blob, repo, &id, 8192);
598 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
602 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
608 got_object_blob_close(blob);
621 static const struct got_error *
622 apply_patch(int *overlapcnt, struct got_worktree *worktree,
623 struct got_repository *repo, struct got_fileindex *fileindex,
624 const char *old, const char *new, struct got_patch *p, int nop,
625 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
627 const struct got_error *err = NULL;
628 int do_merge = 0, file_renamed = 0;
629 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
630 char *oldpath = NULL, *newpath = NULL;
631 char *tmppath = NULL, *template = NULL, *parent = NULL;
632 char *apath = NULL, *mergepath = NULL;
633 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
636 mode_t mode = GOT_DEFAULT_FILE_MODE;
640 /* don't run the diff3 merge on creations/deletions */
641 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
642 err = open_blob(&apath, &afile, p->blob, repo);
644 * ignore failures to open this blob, we might have
647 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
649 else if (err == NULL)
654 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
656 err = got_error_from_errno("asprintf");
660 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
662 err = got_error_from_errno("asprintf");
666 file_renamed = strcmp(oldpath, newpath);
668 if (asprintf(&template, "%s/got-patch",
669 got_worktree_get_root_path(worktree)) == -1) {
670 err = got_error_from_errno(template);
674 if (p->old != NULL && (oldfile = fopen(oldpath, "r")) == NULL) {
675 err = got_error_from_errno2("open", oldpath);
679 err = got_opentemp_named(&tmppath, &tmpfile, template);
683 outfd = fileno(tmpfile);
684 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile, &mode);
689 if (fseeko(afile, 0, SEEK_SET) == -1 ||
690 fseeko(oldfile, 0, SEEK_SET) == -1 ||
691 fseeko(tmpfile, 0, SEEK_SET) == -1) {
692 err = got_error_from_errno("fseeko");
696 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
697 err = got_error_from_errno("asprintf");
702 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
703 err = got_error_from_errno("asprintf");
708 if (asprintf(&anclabel, "commit %s", p->cid) == -1) {
709 err = got_error_from_errno("asprintf");
714 err = got_opentemp_named(&mergepath, &mergefile, template);
718 outfd = fileno(mergefile);
720 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
721 oldfile, oldlabel, anclabel, newlabel,
722 GOT_DIFF_ALGORITHM_PATIENCE);
730 if (p->old != NULL && p->new == NULL) {
731 err = got_worktree_patch_schedule_rm(old, repo, worktree,
732 fileindex, patch_delete, pa);
736 if (fchmod(outfd, mode) == -1) {
737 err = got_error_from_errno2("chmod", tmppath);
741 if (rename(outpath, newpath) == -1) {
742 if (errno != ENOENT) {
743 err = got_error_from_errno3("rename", outpath,
748 err = got_path_dirname(&parent, newpath);
751 err = got_path_mkdir(parent);
754 if (rename(outpath, newpath) == -1) {
755 err = got_error_from_errno3("rename", outpath,
762 err = got_worktree_patch_schedule_rm(old, repo, worktree,
763 fileindex, patch_delete, pa);
765 err = got_worktree_patch_schedule_add(new, repo,
766 worktree, fileindex, patch_add,
770 } else if (p->old == NULL) {
771 err = got_worktree_patch_schedule_add(new, repo, worktree,
772 fileindex, patch_add, pa);
775 } else if (*overlapcnt != 0)
776 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
778 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
780 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
788 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
789 err = got_error_from_errno("fclose");
793 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
794 err = got_error_from_errno("fclose");
798 if (afile != NULL && fclose(afile) == EOF && err == NULL)
799 err = got_error_from_errno("fclose");
802 if (mergepath != NULL)
804 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
805 err = got_error_from_errno("fclose");
816 reverse_patch(struct got_patch *p)
818 struct got_patch_hunk *h;
822 STAILQ_FOREACH(h, &p->head, entries) {
824 h->old_from = h->new_from;
828 h->old_lines = h->new_lines;
832 h->old_nonl = h->new_nonl;
835 for (i = 0; i < h->len; ++i) {
836 if (*h->lines[i] == '+')
838 else if (*h->lines[i] == '-')
844 const struct got_error *
845 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
846 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
847 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
849 const struct got_error *err = NULL, *complete_err = NULL;
850 struct got_fileindex *fileindex = NULL;
851 char *fileindex_path = NULL;
852 char *oldpath, *newpath;
853 struct imsgbuf *ibuf;
854 int imsg_fds[2] = {-1, -1};
855 int overlapcnt, done = 0, failed = 0;
858 ibuf = calloc(1, sizeof(*ibuf));
860 err = got_error_from_errno("calloc");
864 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
865 err = got_error_from_errno("socketpair");
871 err = got_error_from_errno("fork");
873 } else if (pid == 0) {
874 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
879 if (close(imsg_fds[1]) == -1) {
880 err = got_error_from_errno("close");
884 imsg_init(ibuf, imsg_fds[0]);
886 err = send_patch(ibuf, fd);
891 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
896 while (!done && err == NULL) {
898 struct patch_args pa;
900 pa.progress_cb = progress_cb;
901 pa.progress_arg = progress_arg;
904 err = recv_patch(ibuf, &done, &p, strip);
911 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
912 &newpath, worktree, repo, fileindex);
914 err = apply_patch(&overlapcnt, worktree, repo,
915 fileindex, oldpath, newpath, &p, nop, &pa,
916 cancel_cb, cancel_arg);
919 /* recoverable errors */
920 if (err->code == GOT_ERR_FILE_STATUS ||
921 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
922 err = report_progress(&pa, p.old, p.new,
923 GOT_STATUS_CANNOT_UPDATE, err);
924 else if (err->code == GOT_ERR_HUNK_FAILED)
925 err = report_progress(&pa, p.old, p.new,
926 GOT_STATUS_CANNOT_UPDATE, NULL);
940 if (fileindex != NULL)
941 complete_err = got_worktree_patch_complete(fileindex,
943 if (complete_err && err == NULL)
945 free(fileindex_path);
946 if (fd != -1 && close(fd) == -1 && err == NULL)
947 err = got_error_from_errno("close");
950 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
951 err = got_error_from_errno("close");
952 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
953 err = got_error_from_errno("close");
954 if (err == NULL && failed)
955 err = got_error(GOT_ERR_PATCH_FAILED);