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 are still missing:
19 * + "No final newline" handling
21 * Things that we may want to support:
22 * + support indented patches?
23 * + support other kinds of patches?
26 #include <sys/types.h>
27 #include <sys/queue.h>
28 #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;
68 STAILQ_HEAD(, got_patch_hunk) head;
71 static const struct got_error *
72 send_patch(struct imsgbuf *ibuf, int fd)
74 const struct got_error *err = NULL;
76 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
78 err = got_error_from_errno(
79 "imsg_compose GOT_IMSG_PATCH_FILE");
84 if (imsg_flush(ibuf) == -1) {
85 err = got_error_from_errno("imsg_flush");
93 patch_free(struct got_patch *p)
95 struct got_patch_hunk *h;
98 while (!STAILQ_EMPTY(&p->head)) {
99 h = STAILQ_FIRST(&p->head);
100 STAILQ_REMOVE_HEAD(&p->head, entries);
102 for (i = 0; i < h->len; ++i)
112 static const struct got_error *
113 pushline(struct got_patch_hunk *h, const char *line)
118 if (h->len == h->cap) {
119 if ((newcap = h->cap * 1.5) == 0)
121 t = recallocarray(h->lines, h->cap, newcap,
122 sizeof(h->lines[0]));
124 return got_error_from_errno("recallocarray");
129 if ((t = strdup(line)) == NULL)
130 return got_error_from_errno("strdup");
132 h->lines[h->len++] = t;
136 static const struct got_error *
137 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
139 const struct got_error *err = NULL;
141 struct got_imsg_patch_hunk hdr;
142 struct got_imsg_patch patch;
143 struct got_patch_hunk *h = NULL;
146 memset(p, 0, sizeof(*p));
147 STAILQ_INIT(&p->head);
149 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
152 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
156 if (imsg.hdr.type != GOT_IMSG_PATCH) {
157 err = got_error(GOT_ERR_PRIVSEP_MSG);
160 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
161 if (datalen != sizeof(patch)) {
162 err = got_error(GOT_ERR_PRIVSEP_LEN);
165 memcpy(&patch, imsg.data, sizeof(patch));
166 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
167 err = got_error_from_errno("strdup");
170 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
171 err = got_error_from_errno("strdup");
180 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
184 switch (imsg.hdr.type) {
185 case GOT_IMSG_PATCH_DONE:
187 case GOT_IMSG_PATCH_HUNK:
188 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
189 if (datalen != sizeof(hdr)) {
190 err = got_error(GOT_ERR_PRIVSEP_LEN);
193 memcpy(&hdr, imsg.data, sizeof(hdr));
194 if ((h = calloc(1, sizeof(*h))) == NULL) {
195 err = got_error_from_errno("calloc");
198 h->old_from = hdr.oldfrom;
199 h->old_lines = hdr.oldlines;
200 h->new_from = hdr.newfrom;
201 h->new_lines = hdr.newlines;
202 STAILQ_INSERT_TAIL(&p->head, h, entries);
204 case GOT_IMSG_PATCH_LINE:
206 err = got_error(GOT_ERR_PRIVSEP_MSG);
209 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
211 /* at least one char plus newline */
212 if (datalen < 2 || t[datalen-1] != '\0') {
213 err = got_error(GOT_ERR_PRIVSEP_MSG);
216 if (*t != ' ' && *t != '-' && *t != '+') {
217 err = got_error(GOT_ERR_PRIVSEP_MSG);
220 err = pushline(h, t);
225 err = got_error(GOT_ERR_PRIVSEP_MSG);
238 * Copy data from orig starting at copypos until pos into tmp.
239 * If pos is -1, copy until EOF.
241 static const struct got_error *
242 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
247 if (fseek(orig, copypos, SEEK_SET) == -1)
248 return got_error_from_errno("fseek");
250 while (pos == -1 || copypos < pos) {
253 len = MIN(len, (size_t)pos - copypos);
254 r = fread(buf, 1, len, orig);
255 if (r != len && ferror(orig))
256 return got_error_from_errno("fread");
257 w = fwrite(buf, 1, r, tmp);
259 return got_error_from_errno("fwrite");
261 if (r != len && feof(orig)) {
264 return got_error(GOT_ERR_PATCH_DONT_APPLY);
270 static const struct got_error *
271 locate_hunk(FILE *orig, struct got_patch_hunk *h, long *lineno)
273 const struct got_error *err = NULL;
275 char mode = *h->lines[0];
279 long match_lineno = -1;
282 linelen = getline(&line, &linesize, orig);
285 err = got_error_from_errno("getline");
286 else if (match == -1)
287 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
292 if ((mode == ' ' && !strcmp(h->lines[0]+1, line)) ||
293 (mode == '-' && !strcmp(h->lines[0]+1, line)) ||
294 (mode == '+' && *lineno == h->old_from)) {
295 match = ftello(orig);
297 err = got_error_from_errno("ftello");
301 match_lineno = (*lineno)-1;
304 if (*lineno >= h->old_from && match != -1)
309 *lineno = match_lineno;
310 if (fseek(orig, match, SEEK_SET) == -1)
311 err = got_error_from_errno("fseek");
318 static const struct got_error *
319 test_hunk(FILE *orig, struct got_patch_hunk *h)
321 const struct got_error *err = NULL;
323 size_t linesize = 0, i = 0;
326 for (i = 0; i < h->len; ++i) {
327 switch (*h->lines[i]) {
332 linelen = getline(&line, &linesize, orig);
335 err = got_error_from_errno("getline");
338 GOT_ERR_PATCH_DONT_APPLY);
341 if (strcmp(h->lines[i]+1, line)) {
342 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
354 static const struct got_error *
355 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
359 for (i = 0; i < h->len; ++i) {
360 switch (*h->lines[i]) {
362 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
363 return got_error_from_errno("fprintf");
369 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
370 return got_error_from_errno("fprintf");
377 static const struct got_error *
378 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
379 struct got_patch *p, got_worktree_delete_cb delete_cb, void *delete_arg,
380 got_worktree_checkout_cb add_cb, void *add_arg)
382 const struct got_error *err = NULL;
383 struct got_pathlist_head paths;
384 struct got_pathlist_entry *pe;
385 char *path = NULL, *tmppath = NULL, *template = NULL;
386 FILE *orig = NULL, *tmp = NULL;
387 struct got_patch_hunk *h;
397 if (p->old == NULL && p->new == NULL)
398 return got_error(GOT_ERR_PATCH_MALFORMED);
400 err = got_worktree_resolve_path(&path, worktree,
401 p->new != NULL ? p->new : p->old);
404 err = got_pathlist_insert(&pe, &paths, path, NULL);
408 if (p->old != NULL && p->new == NULL) {
410 * special case: delete a file. don't try to match
411 * the lines but just schedule the removal.
413 err = got_worktree_schedule_delete(worktree, &paths,
414 0, NULL, delete_cb, delete_arg, repo, 0, 0);
416 } else if (p->old != NULL && strcmp(p->old, p->new)) {
417 err = got_error(GOT_ERR_PATCH_PATHS_DIFFER);
421 if (asprintf(&template, "%s/got-patch",
422 got_worktree_get_root_path(worktree)) == -1) {
423 err = got_error_from_errno(template);
427 err = got_opentemp_named(&tmppath, &tmp, template);
431 if (p->old == NULL) { /* create */
432 h = STAILQ_FIRST(&p->head);
433 if (h == NULL || STAILQ_NEXT(h, entries) != NULL) {
434 err = got_error(GOT_ERR_PATCH_MALFORMED);
437 for (i = 0; i < h->len; ++i) {
438 if (fprintf(tmp, "%s", h->lines[i]+1) < 0) {
439 err = got_error_from_errno("fprintf");
446 if ((orig = fopen(path, "r")) == NULL) {
447 err = got_error_from_errno2("fopen", path);
452 STAILQ_FOREACH(h, &p->head, entries) {
454 err = locate_hunk(orig, h, &lineno);
457 if ((pos = ftello(orig)) == -1) {
458 err = got_error_from_errno("ftello");
461 err = copy(tmp, orig, copypos, pos);
466 err = test_hunk(orig, h);
467 if (err != NULL && err->code == GOT_ERR_PATCH_DONT_APPLY) {
469 * try to apply the hunk again starting the search
470 * after the previous partial match.
472 if (fseek(orig, pos, SEEK_SET) == -1) {
473 err = got_error_from_errno("fseek");
476 linelen = getline(&line, &linesize, orig);
478 err = got_error_from_errno("getline");
487 err = apply_hunk(tmp, h, &lineno);
491 copypos = ftello(orig);
493 err = got_error_from_errno("ftello");
499 err = copy(tmp, orig, copypos, -1);
505 if (rename(tmppath, path) == -1) {
506 err = got_error_from_errno3("rename", tmppath, path);
511 err = got_worktree_schedule_add(worktree, &paths,
512 add_cb, add_arg, repo, 1);
514 printf("M %s\n", path); /* XXX */
517 if (err != NULL && p->old == NULL && path != NULL)
525 if (p->old == NULL && err != NULL)
531 got_pathlist_free(&paths);
535 const struct got_error *
536 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
537 got_worktree_delete_cb delete_cb, void *delete_arg,
538 got_worktree_checkout_cb add_cb, void *add_arg)
540 const struct got_error *err = NULL;
541 struct imsgbuf *ibuf;
542 int imsg_fds[2] = {-1, -1};
546 ibuf = calloc(1, sizeof(*ibuf));
548 err = got_error_from_errno("calloc");
552 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
553 err = got_error_from_errno("socketpair");
559 err = got_error_from_errno("fork");
561 } else if (pid == 0) {
562 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
567 if (close(imsg_fds[1]) == -1) {
568 err = got_error_from_errno("close");
572 imsg_init(ibuf, imsg_fds[0]);
574 err = send_patch(ibuf, fd);
579 while (!done && err == NULL) {
582 err = recv_patch(ibuf, &done, &p);
586 err = apply_patch(worktree, repo, &p, delete_cb, delete_arg,
594 if (fd != -1 && close(fd) == -1 && err == NULL)
595 err = got_error_from_errno("close");
598 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
599 err = got_error_from_errno("close");
600 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
601 err = got_error_from_errno("close");