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_opentemp.h"
43 #include "got_patch.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
47 #include "got_lib_privsep.h"
49 #define MIN(a, b) ((a) < (b) ? (a) : (b))
51 struct got_patch_hunk {
52 STAILQ_ENTRY(got_patch_hunk) entries;
53 const struct got_error *err;
65 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
69 struct got_patch_hunk_head head;
73 got_patch_progress_cb progress_cb;
75 struct got_patch_hunk_head *head;
78 static const struct got_error *
79 send_patch(struct imsgbuf *ibuf, int fd)
81 const struct got_error *err = NULL;
83 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
85 err = got_error_from_errno(
86 "imsg_compose GOT_IMSG_PATCH_FILE");
91 if (imsg_flush(ibuf) == -1) {
92 err = got_error_from_errno("imsg_flush");
100 patch_free(struct got_patch *p)
102 struct got_patch_hunk *h;
105 while (!STAILQ_EMPTY(&p->head)) {
106 h = STAILQ_FIRST(&p->head);
107 STAILQ_REMOVE_HEAD(&p->head, entries);
109 for (i = 0; i < h->len; ++i)
119 static const struct got_error *
120 pushline(struct got_patch_hunk *h, const char *line)
125 if (h->len == h->cap) {
126 if ((newcap = h->cap * 1.5) == 0)
128 t = recallocarray(h->lines, h->cap, newcap,
129 sizeof(h->lines[0]));
131 return got_error_from_errno("recallocarray");
136 if ((t = strdup(line)) == NULL)
137 return got_error_from_errno("strdup");
139 h->lines[h->len++] = t;
143 static const struct got_error *
144 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
146 const struct got_error *err = NULL;
148 struct got_imsg_patch_hunk hdr;
149 struct got_imsg_patch patch;
150 struct got_patch_hunk *h = NULL;
153 memset(p, 0, sizeof(*p));
154 STAILQ_INIT(&p->head);
156 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
159 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
163 if (imsg.hdr.type != GOT_IMSG_PATCH) {
164 err = got_error(GOT_ERR_PRIVSEP_MSG);
167 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
168 if (datalen != sizeof(patch)) {
169 err = got_error(GOT_ERR_PRIVSEP_LEN);
172 memcpy(&patch, imsg.data, sizeof(patch));
174 /* automatically set strip=1 for git-style diffs */
175 if (strip == -1 && patch.git &&
176 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
177 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
180 /* prefer the new name if not /dev/null for not git-style diffs */
181 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
182 err = got_path_strip(&p->old, patch.new, strip);
185 } else if (*patch.old != '\0') {
186 err = got_path_strip(&p->old, patch.old, strip);
191 if (*patch.new != '\0') {
192 err = got_path_strip(&p->new, patch.new, strip);
197 if (p->old == NULL && p->new == NULL) {
198 err = got_error(GOT_ERR_PATCH_MALFORMED);
207 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
211 switch (imsg.hdr.type) {
212 case GOT_IMSG_PATCH_DONE:
214 case GOT_IMSG_PATCH_HUNK:
215 if (h != NULL && h->nonl) {
216 err = got_error(GOT_ERR_PATCH_MALFORMED);
219 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
220 if (datalen != sizeof(hdr)) {
221 err = got_error(GOT_ERR_PRIVSEP_LEN);
224 memcpy(&hdr, imsg.data, sizeof(hdr));
225 if ((h = calloc(1, sizeof(*h))) == NULL) {
226 err = got_error_from_errno("calloc");
229 h->old_from = hdr.oldfrom;
230 h->old_lines = hdr.oldlines;
231 h->new_from = hdr.newfrom;
232 h->new_lines = hdr.newlines;
233 STAILQ_INSERT_TAIL(&p->head, h, entries);
235 case GOT_IMSG_PATCH_LINE:
237 err = got_error(GOT_ERR_PRIVSEP_MSG);
240 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
242 /* at least one char */
243 if (datalen < 2 || t[datalen-1] != '\0') {
244 err = got_error(GOT_ERR_PRIVSEP_MSG);
247 if (*t != ' ' && *t != '-' && *t != '+' &&
249 err = got_error(GOT_ERR_PRIVSEP_MSG);
253 err = got_error(GOT_ERR_PATCH_MALFORMED);
257 err = pushline(h, t);
262 err = got_error(GOT_ERR_PRIVSEP_MSG);
275 * Copy data from orig starting at copypos until pos into tmp.
276 * If pos is -1, copy until EOF.
278 static const struct got_error *
279 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
284 if (fseek(orig, copypos, SEEK_SET) == -1)
285 return got_error_from_errno("fseek");
287 while (pos == -1 || copypos < pos) {
290 len = MIN(len, (size_t)pos - copypos);
291 r = fread(buf, 1, len, orig);
292 if (r != len && ferror(orig))
293 return got_error_from_errno("fread");
294 w = fwrite(buf, 1, r, tmp);
296 return got_error_from_errno("fwrite");
298 if (r != len && feof(orig)) {
301 return got_error(GOT_ERR_HUNK_FAILED);
307 static const struct got_error *
308 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
310 const struct got_error *err = NULL;
312 char mode = *h->lines[0];
316 long match_lineno = -1;
319 linelen = getline(&line, &linesize, orig);
322 err = got_error_from_errno("getline");
323 else if (match == -1)
324 err = got_error(GOT_ERR_HUNK_FAILED);
327 if (line[linelen - 1] == '\n')
328 line[linelen - 1] = '\0';
331 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
332 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
333 (mode == '+' && *lineno == h->old_from)) {
334 match = ftello(orig);
336 err = got_error_from_errno("ftello");
340 match_lineno = (*lineno)-1;
343 if (*lineno >= h->old_from && match != -1)
349 *lineno = match_lineno;
350 if (fseek(orig, match, SEEK_SET) == -1)
351 err = got_error_from_errno("fseek");
358 static const struct got_error *
359 test_hunk(FILE *orig, struct got_patch_hunk *h)
361 const struct got_error *err = NULL;
363 size_t linesize = 0, i = 0;
366 for (i = 0; i < h->len; ++i) {
367 switch (*h->lines[i]) {
372 linelen = getline(&line, &linesize, orig);
375 err = got_error_from_errno("getline");
378 GOT_ERR_HUNK_FAILED);
381 if (line[linelen - 1] == '\n')
382 line[linelen - 1] = '\0';
383 if (strcmp(h->lines[i] + 1, line)) {
384 err = got_error(GOT_ERR_HUNK_FAILED);
396 static const struct got_error *
397 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
401 for (i = 0; i < h->len; ++i) {
402 switch (*h->lines[i]) {
404 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
405 return got_error_from_errno("fprintf");
411 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
412 return got_error_from_errno("fprintf");
413 if (i != h->len - 1 || !h->nonl) {
414 if (fprintf(tmp, "\n") < 0)
415 return got_error_from_errno(
424 static const struct got_error *
425 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
428 const struct got_error *err = NULL;
429 struct got_patch_hunk *h;
438 if (p->old == NULL) { /* create */
439 h = STAILQ_FIRST(&p->head);
440 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
441 return got_error(GOT_ERR_PATCH_MALFORMED);
444 return apply_hunk(tmp, h, &lineno);
447 if ((orig = fopen(path, "r")) == NULL) {
448 err = got_error_from_errno2("fopen", path);
452 if (fstat(fileno(orig), &sb) == -1) {
453 err = got_error_from_errno("fstat");
459 STAILQ_FOREACH(h, &p->head, entries) {
460 if (h->lines == NULL)
464 err = locate_hunk(orig, h, &pos, &lineno);
465 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
470 err = copy(tmp, orig, copypos, pos);
475 err = test_hunk(orig, h);
476 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
478 * try to apply the hunk again starting the search
479 * after the previous partial match.
481 if (fseek(orig, pos, SEEK_SET) == -1) {
482 err = got_error_from_errno("fseek");
485 linelen = getline(&line, &linesize, orig);
487 err = got_error_from_errno("getline");
496 if (lineno + 1 != h->old_from)
497 h->offset = lineno + 1 - h->old_from;
500 err = apply_hunk(tmp, h, &lineno);
504 copypos = ftello(orig);
506 err = got_error_from_errno("ftello");
511 if (p->new == NULL && sb.st_size != copypos) {
512 h = STAILQ_FIRST(&p->head);
513 h->err = got_error(GOT_ERR_HUNK_FAILED);
515 } else if (!nop && !feof(orig))
516 err = copy(tmp, orig, copypos, -1);
524 static const struct got_error *
525 report_progress(struct patch_args *pa, const char *old, const char *new,
526 unsigned char status, const struct got_error *orig_error)
528 const struct got_error *err;
529 struct got_patch_hunk *h;
531 err = pa->progress_cb(pa->progress_arg, old, new, status,
532 orig_error, 0, 0, 0, 0, 0, NULL);
536 STAILQ_FOREACH(h, pa->head, entries) {
537 if (h->offset == 0 && h->err == NULL)
540 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
541 h->old_from, h->old_lines, h->new_from, h->new_lines,
550 static const struct got_error *
551 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
554 return report_progress(arg, path, NULL, status, NULL);
557 static const struct got_error *
558 patch_add(void *arg, unsigned char status, const char *path)
560 return report_progress(arg, NULL, path, status, NULL);
563 static const struct got_error *
564 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
565 const char *oldpath, const char *newpath, struct got_patch *p,
566 int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
568 const struct got_error *err = NULL;
569 struct got_pathlist_head oldpaths, newpaths;
570 struct got_pathlist_entry *pe;
571 int file_renamed = 0;
572 char *tmppath = NULL, *template = NULL, *parent = NULL;;
574 mode_t mode = GOT_DEFAULT_FILE_MODE;
576 TAILQ_INIT(&oldpaths);
577 TAILQ_INIT(&newpaths);
579 err = got_pathlist_insert(&pe, &oldpaths, oldpath, NULL);
582 err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
586 file_renamed = strcmp(oldpath, newpath);
588 if (asprintf(&template, "%s/got-patch",
589 got_worktree_get_root_path(worktree)) == -1) {
590 err = got_error_from_errno(template);
595 err = got_opentemp_named(&tmppath, &tmp, template);
598 err = patch_file(p, oldpath, tmp, nop, &mode);
605 if (p->old != NULL && p->new == NULL) {
606 err = got_worktree_schedule_delete(worktree, &oldpaths,
607 0, NULL, patch_delete, pa, repo, 0, 0);
611 if (fchmod(fileno(tmp), mode) == -1) {
612 err = got_error_from_errno2("chmod", newpath);
616 if (rename(tmppath, newpath) == -1) {
617 if (errno != ENOENT) {
618 err = got_error_from_errno3("rename", tmppath,
623 err = got_path_dirname(&parent, newpath);
626 err = got_path_mkdir(parent);
629 if (rename(tmppath, newpath) == -1) {
630 err = got_error_from_errno3("rename", tmppath,
637 err = got_worktree_schedule_delete(worktree, &oldpaths,
638 0, NULL, patch_delete, pa, repo, 0, 0);
640 err = got_worktree_schedule_add(worktree, &newpaths,
641 patch_add, pa, repo, 1);
644 } else if (p->old == NULL) {
645 err = got_worktree_schedule_add(worktree, &newpaths,
646 patch_add, pa, repo, 1);
650 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
654 got_pathlist_free(&oldpaths);
655 got_pathlist_free(&newpaths);
664 const struct got_error *
665 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
666 int nop, int strip, got_patch_progress_cb progress_cb, void *progress_arg,
667 got_cancel_cb cancel_cb, void *cancel_arg)
669 const struct got_error *err = NULL;
670 struct got_fileindex *fileindex = NULL;
671 char *oldpath, *newpath;
672 struct imsgbuf *ibuf;
673 int imsg_fds[2] = {-1, -1};
674 int done = 0, failed = 0;
677 ibuf = calloc(1, sizeof(*ibuf));
679 err = got_error_from_errno("calloc");
683 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
684 err = got_error_from_errno("socketpair");
690 err = got_error_from_errno("fork");
692 } else if (pid == 0) {
693 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
698 if (close(imsg_fds[1]) == -1) {
699 err = got_error_from_errno("close");
703 imsg_init(ibuf, imsg_fds[0]);
705 err = send_patch(ibuf, fd);
710 err = got_worktree_patch_prepare(&fileindex, worktree);
714 while (!done && err == NULL) {
716 struct patch_args pa;
718 pa.progress_cb = progress_cb;
719 pa.progress_arg = progress_arg;
722 err = recv_patch(ibuf, &done, &p, strip);
726 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
727 &newpath, worktree, repo, fileindex);
729 err = apply_patch(worktree, repo, oldpath, newpath,
730 &p, nop, &pa, cancel_cb, cancel_arg);
733 /* recoverable errors */
734 if (err->code == GOT_ERR_FILE_STATUS ||
735 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
736 err = report_progress(&pa, p.old, p.new,
737 GOT_STATUS_CANNOT_UPDATE, err);
738 else if (err->code == GOT_ERR_HUNK_FAILED)
739 err = report_progress(&pa, p.old, p.new,
740 GOT_STATUS_CANNOT_UPDATE, NULL);
753 got_worktree_patch_complete(fileindex);
754 if (fd != -1 && close(fd) == -1 && err == NULL)
755 err = got_error_from_errno("close");
758 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
759 err = got_error_from_errno("close");
760 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
761 err = got_error_from_errno("close");
762 if (err == NULL && failed)
763 err = got_error(GOT_ERR_PATCH_FAILED);