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_opentemp.h"
46 #include "got_patch.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_privsep.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);
73 struct got_patch_hunk_head head;
77 got_patch_progress_cb progress_cb;
79 struct got_patch_hunk_head *head;
82 static const struct got_error *
83 send_patch(struct imsgbuf *ibuf, int fd)
85 const struct got_error *err = NULL;
87 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
89 err = got_error_from_errno(
90 "imsg_compose GOT_IMSG_PATCH_FILE");
95 if (imsg_flush(ibuf) == -1) {
96 err = got_error_from_errno("imsg_flush");
104 patch_free(struct got_patch *p)
106 struct got_patch_hunk *h;
109 while (!STAILQ_EMPTY(&p->head)) {
110 h = STAILQ_FIRST(&p->head);
111 STAILQ_REMOVE_HEAD(&p->head, entries);
113 for (i = 0; i < h->len; ++i)
122 memset(p, 0, sizeof(*p));
123 STAILQ_INIT(&p->head);
126 static const struct got_error *
127 pushline(struct got_patch_hunk *h, const char *line)
132 if (h->len == h->cap) {
133 if ((newcap = h->cap * 1.5) == 0)
135 t = recallocarray(h->lines, h->cap, newcap,
136 sizeof(h->lines[0]));
138 return got_error_from_errno("recallocarray");
143 if ((t = strdup(line)) == NULL)
144 return got_error_from_errno("strdup");
146 h->lines[h->len++] = t;
150 static const struct got_error *
151 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
153 const struct got_error *err = NULL;
155 struct got_imsg_patch_hunk hdr;
156 struct got_imsg_patch patch;
157 struct got_patch_hunk *h = NULL;
161 memset(p, 0, sizeof(*p));
162 STAILQ_INIT(&p->head);
164 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
167 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
171 if (imsg.hdr.type != GOT_IMSG_PATCH) {
172 err = got_error(GOT_ERR_PRIVSEP_MSG);
175 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
176 if (datalen != sizeof(patch)) {
177 err = got_error(GOT_ERR_PRIVSEP_LEN);
180 memcpy(&patch, imsg.data, sizeof(patch));
182 if (patch.old[sizeof(patch.old)-1] != '\0' ||
183 patch.new[sizeof(patch.new)-1] != '\0') {
184 err = got_error(GOT_ERR_PRIVSEP_LEN);
188 /* automatically set strip=1 for git-style diffs */
189 if (strip == -1 && patch.git &&
190 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
191 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
194 /* prefer the new name if not /dev/null for not git-style diffs */
195 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
196 err = got_path_strip(&p->old, patch.new, strip);
199 } else if (*patch.old != '\0') {
200 err = got_path_strip(&p->old, patch.old, strip);
205 if (*patch.new != '\0') {
206 err = got_path_strip(&p->new, patch.new, strip);
211 if (p->old == NULL && p->new == NULL) {
212 err = got_error(GOT_ERR_PATCH_MALFORMED);
221 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
227 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
228 switch (imsg.hdr.type) {
229 case GOT_IMSG_PATCH_DONE:
230 if (h != NULL && h->len == 0)
231 err = got_error(GOT_ERR_PATCH_MALFORMED);
233 case GOT_IMSG_PATCH_HUNK:
235 (h->len == 0 || h->old_nonl || h->new_nonl)) {
236 err = got_error(GOT_ERR_PATCH_MALFORMED);
240 if (datalen != sizeof(hdr)) {
241 err = got_error(GOT_ERR_PRIVSEP_LEN);
244 memcpy(&hdr, imsg.data, sizeof(hdr));
245 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
246 err = got_error(GOT_ERR_PRIVSEP_LEN);
249 if ((h = calloc(1, sizeof(*h))) == NULL) {
250 err = got_error_from_errno("calloc");
253 h->old_from = hdr.oldfrom;
254 h->old_lines = hdr.oldlines;
255 h->new_from = hdr.newfrom;
256 h->new_lines = hdr.newlines;
257 STAILQ_INSERT_TAIL(&p->head, h, entries);
259 case GOT_IMSG_PATCH_LINE:
261 err = got_error(GOT_ERR_PRIVSEP_MSG);
265 /* at least one char */
266 if (datalen < 2 || t[datalen-1] != '\0') {
267 err = got_error(GOT_ERR_PRIVSEP_MSG);
270 if (*t != ' ' && *t != '-' && *t != '+' &&
272 err = got_error(GOT_ERR_PRIVSEP_MSG);
277 err = pushline(h, t);
278 else if (lastmode == '-')
280 else if (lastmode == '+')
283 err = got_error(GOT_ERR_PATCH_MALFORMED);
291 err = got_error(GOT_ERR_PRIVSEP_MSG);
307 * Copy data from orig starting at copypos until pos into tmp.
308 * If pos is -1, copy until EOF.
310 static const struct got_error *
311 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
316 if (fseeko(orig, copypos, SEEK_SET) == -1)
317 return got_error_from_errno("fseeko");
319 while (pos == -1 || copypos < pos) {
322 len = MIN(len, (size_t)pos - copypos);
323 r = fread(buf, 1, len, orig);
324 if (r != len && ferror(orig))
325 return got_error_from_errno("fread");
326 w = fwrite(buf, 1, r, tmp);
328 return got_error_from_errno("fwrite");
330 if (r != len && feof(orig)) {
333 return got_error(GOT_ERR_HUNK_FAILED);
339 static const struct got_error *
340 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
342 const struct got_error *err = NULL;
344 char mode = *h->lines[0];
348 int match_lineno = -1;
351 linelen = getline(&line, &linesize, orig);
354 err = got_error_from_errno("getline");
355 else if (match == -1)
356 err = got_error(GOT_ERR_HUNK_FAILED);
359 if (line[linelen - 1] == '\n')
360 line[linelen - 1] = '\0';
363 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
364 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
365 (mode == '+' && *lineno == h->old_from)) {
366 match = ftello(orig);
368 err = got_error_from_errno("ftello");
372 match_lineno = (*lineno)-1;
375 if (*lineno >= h->old_from && match != -1)
381 *lineno = match_lineno;
382 if (fseeko(orig, match, SEEK_SET) == -1)
383 err = got_error_from_errno("fseeko");
390 static const struct got_error *
391 test_hunk(FILE *orig, struct got_patch_hunk *h)
393 const struct got_error *err = NULL;
395 size_t linesize = 0, i = 0;
398 for (i = 0; i < h->len; ++i) {
399 switch (*h->lines[i]) {
404 linelen = getline(&line, &linesize, orig);
407 err = got_error_from_errno("getline");
410 GOT_ERR_HUNK_FAILED);
413 if (line[linelen - 1] == '\n')
414 line[linelen - 1] = '\0';
415 if (strcmp(h->lines[i] + 1, line)) {
416 err = got_error(GOT_ERR_HUNK_FAILED);
428 static const struct got_error *
429 apply_hunk(FILE *tmp, struct got_patch_hunk *h, int *lineno)
433 for (i = 0; i < h->len; ++i) {
434 switch (*h->lines[i]) {
436 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
437 return got_error_from_errno("fprintf");
444 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
445 return got_error_from_errno("fprintf");
446 if (new != h->new_lines || !h->new_nonl) {
447 if (fprintf(tmp, "\n") < 0)
448 return got_error_from_errno(
457 static const struct got_error *
458 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
461 const struct got_error *err = NULL;
462 struct got_patch_hunk *h;
471 if (p->old == NULL) { /* create */
472 h = STAILQ_FIRST(&p->head);
473 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
474 return got_error(GOT_ERR_PATCH_MALFORMED);
477 return apply_hunk(tmp, h, &lineno);
480 if ((orig = fopen(path, "r")) == NULL) {
481 err = got_error_from_errno2("fopen", path);
485 if (fstat(fileno(orig), &sb) == -1) {
486 err = got_error_from_errno("fstat");
492 STAILQ_FOREACH(h, &p->head, entries) {
494 err = locate_hunk(orig, h, &pos, &lineno);
495 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
500 err = copy(tmp, orig, copypos, pos);
505 err = test_hunk(orig, h);
506 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
508 * try to apply the hunk again starting the search
509 * after the previous partial match.
511 if (fseeko(orig, pos, SEEK_SET) == -1) {
512 err = got_error_from_errno("fseeko");
515 linelen = getline(&line, &linesize, orig);
517 err = got_error_from_errno("getline");
526 if (lineno + 1 != h->old_from)
527 h->offset = lineno + 1 - h->old_from;
530 err = apply_hunk(tmp, h, &lineno);
534 copypos = ftello(orig);
536 err = got_error_from_errno("ftello");
541 if (p->new == NULL && sb.st_size != copypos) {
542 h = STAILQ_FIRST(&p->head);
543 h->err = got_error(GOT_ERR_HUNK_FAILED);
545 } else if (!nop && !feof(orig))
546 err = copy(tmp, orig, copypos, -1);
549 if (orig != NULL && fclose(orig) == EOF && err == NULL)
550 err = got_error_from_errno("fclose");
554 static const struct got_error *
555 report_progress(struct patch_args *pa, const char *old, const char *new,
556 unsigned char status, const struct got_error *orig_error)
558 const struct got_error *err;
559 struct got_patch_hunk *h;
561 err = pa->progress_cb(pa->progress_arg, old, new, status,
562 orig_error, 0, 0, 0, 0, 0, NULL);
566 STAILQ_FOREACH(h, pa->head, entries) {
567 if (h->offset == 0 && h->err == NULL)
570 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
571 h->old_from, h->old_lines, h->new_from, h->new_lines,
580 static const struct got_error *
581 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
584 return report_progress(arg, path, NULL, status, NULL);
587 static const struct got_error *
588 patch_add(void *arg, unsigned char status, const char *path)
590 return report_progress(arg, NULL, path, status, NULL);
593 static const struct got_error *
594 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
595 struct got_fileindex *fileindex, const char *old, const char *new,
596 struct got_patch *p, int nop, struct patch_args *pa,
597 got_cancel_cb cancel_cb, void *cancel_arg)
599 const struct got_error *err = NULL;
600 int file_renamed = 0;
601 char *oldpath = NULL, *newpath = NULL;
602 char *tmppath = NULL, *template = NULL, *parent = NULL;
604 mode_t mode = GOT_DEFAULT_FILE_MODE;
606 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
608 err = got_error_from_errno("asprintf");
612 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
614 err = got_error_from_errno("asprintf");
618 file_renamed = strcmp(oldpath, newpath);
620 if (asprintf(&template, "%s/got-patch",
621 got_worktree_get_root_path(worktree)) == -1) {
622 err = got_error_from_errno(template);
627 err = got_opentemp_named(&tmppath, &tmp, template);
630 err = patch_file(p, oldpath, tmp, nop, &mode);
637 if (p->old != NULL && p->new == NULL) {
638 err = got_worktree_patch_schedule_rm(old, repo, worktree,
639 fileindex, patch_delete, pa);
643 if (fchmod(fileno(tmp), mode) == -1) {
644 err = got_error_from_errno2("chmod", tmppath);
648 if (rename(tmppath, newpath) == -1) {
649 if (errno != ENOENT) {
650 err = got_error_from_errno3("rename", tmppath,
655 err = got_path_dirname(&parent, newpath);
658 err = got_path_mkdir(parent);
661 if (rename(tmppath, newpath) == -1) {
662 err = got_error_from_errno3("rename", tmppath,
669 err = got_worktree_patch_schedule_rm(old, repo, worktree,
670 fileindex, patch_delete, pa);
672 err = got_worktree_patch_schedule_add(new, repo,
673 worktree, fileindex, patch_add,
677 } else if (p->old == NULL) {
678 err = got_worktree_patch_schedule_add(new, repo, worktree,
679 fileindex, patch_add, pa);
683 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
690 if (tmp != NULL && fclose(tmp) == EOF && err == NULL)
691 err = got_error_from_errno("fclose");
699 reverse_patch(struct got_patch *p)
701 struct got_patch_hunk *h;
705 STAILQ_FOREACH(h, &p->head, entries) {
707 h->old_from = h->new_from;
711 h->old_lines = h->new_lines;
715 h->old_nonl = h->new_nonl;
718 for (i = 0; i < h->len; ++i) {
719 if (*h->lines[i] == '+')
721 else if (*h->lines[i] == '-')
727 const struct got_error *
728 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
729 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
730 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
732 const struct got_error *err = NULL, *complete_err = NULL;
733 struct got_fileindex *fileindex = NULL;
734 char *fileindex_path = NULL;
735 char *oldpath, *newpath;
736 struct imsgbuf *ibuf;
737 int imsg_fds[2] = {-1, -1};
738 int done = 0, failed = 0;
741 ibuf = calloc(1, sizeof(*ibuf));
743 err = got_error_from_errno("calloc");
747 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
748 err = got_error_from_errno("socketpair");
754 err = got_error_from_errno("fork");
756 } else if (pid == 0) {
757 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
762 if (close(imsg_fds[1]) == -1) {
763 err = got_error_from_errno("close");
767 imsg_init(ibuf, imsg_fds[0]);
769 err = send_patch(ibuf, fd);
774 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
779 while (!done && err == NULL) {
781 struct patch_args pa;
783 pa.progress_cb = progress_cb;
784 pa.progress_arg = progress_arg;
787 err = recv_patch(ibuf, &done, &p, strip);
794 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
795 &newpath, worktree, repo, fileindex);
797 err = apply_patch(worktree, repo, fileindex, oldpath,
798 newpath, &p, nop, &pa, cancel_cb, cancel_arg);
801 /* recoverable errors */
802 if (err->code == GOT_ERR_FILE_STATUS ||
803 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
804 err = report_progress(&pa, p.old, p.new,
805 GOT_STATUS_CANNOT_UPDATE, err);
806 else if (err->code == GOT_ERR_HUNK_FAILED)
807 err = report_progress(&pa, p.old, p.new,
808 GOT_STATUS_CANNOT_UPDATE, NULL);
820 if (fileindex != NULL)
821 complete_err = got_worktree_patch_complete(fileindex,
823 if (complete_err && err == NULL)
825 free(fileindex_path);
826 if (fd != -1 && close(fd) == -1 && err == NULL)
827 err = got_error_from_errno("close");
830 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
831 err = got_error_from_errno("close");
832 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
833 err = got_error_from_errno("close");
834 if (err == NULL && failed)
835 err = got_error(GOT_ERR_PATCH_FAILED);