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>
40 #include "got_error.h"
41 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_opentemp.h"
47 #include "got_patch.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_privsep.h"
53 #define MIN(a, b) ((a) < (b) ? (a) : (b))
55 struct got_patch_hunk {
56 STAILQ_ENTRY(got_patch_hunk) entries;
69 STAILQ_HEAD(, got_patch_hunk) head;
72 static const struct got_error *
73 send_patch(struct imsgbuf *ibuf, int fd)
75 const struct got_error *err = NULL;
77 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
79 err = got_error_from_errno(
80 "imsg_compose GOT_IMSG_PATCH_FILE");
85 if (imsg_flush(ibuf) == -1) {
86 err = got_error_from_errno("imsg_flush");
94 patch_free(struct got_patch *p)
96 struct got_patch_hunk *h;
99 while (!STAILQ_EMPTY(&p->head)) {
100 h = STAILQ_FIRST(&p->head);
101 STAILQ_REMOVE_HEAD(&p->head, entries);
103 for (i = 0; i < h->len; ++i)
113 static const struct got_error *
114 pushline(struct got_patch_hunk *h, const char *line)
119 if (h->len == h->cap) {
120 if ((newcap = h->cap * 1.5) == 0)
122 t = recallocarray(h->lines, h->cap, newcap,
123 sizeof(h->lines[0]));
125 return got_error_from_errno("recallocarray");
130 if ((t = strdup(line)) == NULL)
131 return got_error_from_errno("strdup");
133 h->lines[h->len++] = t;
137 static const struct got_error *
138 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
140 const struct got_error *err = NULL;
142 struct got_imsg_patch_hunk hdr;
143 struct got_imsg_patch patch;
144 struct got_patch_hunk *h = NULL;
147 memset(p, 0, sizeof(*p));
148 STAILQ_INIT(&p->head);
150 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
153 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
157 if (imsg.hdr.type != GOT_IMSG_PATCH) {
158 err = got_error(GOT_ERR_PRIVSEP_MSG);
161 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
162 if (datalen != sizeof(patch)) {
163 err = got_error(GOT_ERR_PRIVSEP_LEN);
166 memcpy(&patch, imsg.data, sizeof(patch));
167 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
168 err = got_error_from_errno("strdup");
171 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
172 err = got_error_from_errno("strdup");
181 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
185 switch (imsg.hdr.type) {
186 case GOT_IMSG_PATCH_DONE:
188 case GOT_IMSG_PATCH_HUNK:
189 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
190 if (datalen != sizeof(hdr)) {
191 err = got_error(GOT_ERR_PRIVSEP_LEN);
194 memcpy(&hdr, imsg.data, sizeof(hdr));
195 if ((h = calloc(1, sizeof(*h))) == NULL) {
196 err = got_error_from_errno("calloc");
199 h->old_from = hdr.oldfrom;
200 h->old_lines = hdr.oldlines;
201 h->new_from = hdr.newfrom;
202 h->new_lines = hdr.newlines;
203 STAILQ_INSERT_TAIL(&p->head, h, entries);
205 case GOT_IMSG_PATCH_LINE:
207 err = got_error(GOT_ERR_PRIVSEP_MSG);
210 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
212 /* at least one char plus newline */
213 if (datalen < 2 || t[datalen-1] != '\0') {
214 err = got_error(GOT_ERR_PRIVSEP_MSG);
217 if (*t != ' ' && *t != '-' && *t != '+') {
218 err = got_error(GOT_ERR_PRIVSEP_MSG);
221 err = pushline(h, t);
226 err = got_error(GOT_ERR_PRIVSEP_MSG);
239 * Copy data from orig starting at copypos until pos into tmp.
240 * If pos is -1, copy until EOF.
242 static const struct got_error *
243 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
248 if (fseek(orig, copypos, SEEK_SET) == -1)
249 return got_error_from_errno("fseek");
251 while (pos == -1 || copypos < pos) {
254 len = MIN(len, (size_t)pos - copypos);
255 r = fread(buf, 1, len, orig);
256 if (r != len && ferror(orig))
257 return got_error_from_errno("fread");
258 w = fwrite(buf, 1, r, tmp);
260 return got_error_from_errno("fwrite");
262 if (r != len && feof(orig)) {
265 return got_error(GOT_ERR_PATCH_DONT_APPLY);
271 static const struct got_error *
272 locate_hunk(FILE *orig, struct got_patch_hunk *h, long *lineno)
274 const struct got_error *err = NULL;
276 char mode = *h->lines[0];
280 long match_lineno = -1;
283 linelen = getline(&line, &linesize, orig);
286 err = got_error_from_errno("getline");
287 else if (match == -1)
288 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
293 if ((mode == ' ' && !strcmp(h->lines[0]+1, line)) ||
294 (mode == '-' && !strcmp(h->lines[0]+1, line)) ||
295 (mode == '+' && *lineno == h->old_from)) {
296 match = ftello(orig);
298 err = got_error_from_errno("ftello");
302 match_lineno = (*lineno)-1;
305 if (*lineno >= h->old_from && match != -1)
310 *lineno = match_lineno;
311 if (fseek(orig, match, SEEK_SET) == -1)
312 err = got_error_from_errno("fseek");
319 static const struct got_error *
320 test_hunk(FILE *orig, struct got_patch_hunk *h)
322 const struct got_error *err = NULL;
324 size_t linesize = 0, i = 0;
327 for (i = 0; i < h->len; ++i) {
328 switch (*h->lines[i]) {
333 linelen = getline(&line, &linesize, orig);
336 err = got_error_from_errno("getline");
339 GOT_ERR_PATCH_DONT_APPLY);
342 if (strcmp(h->lines[i]+1, line)) {
343 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
355 static const struct got_error *
356 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
360 for (i = 0; i < h->len; ++i) {
361 switch (*h->lines[i]) {
363 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
364 return got_error_from_errno("fprintf");
370 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
371 return got_error_from_errno("fprintf");
378 static const struct got_error *
379 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
380 struct got_patch *p, got_worktree_delete_cb delete_cb, void *delete_arg,
381 got_worktree_checkout_cb add_cb, void *add_arg)
383 const struct got_error *err = NULL;
384 struct got_pathlist_head paths;
385 struct got_pathlist_entry *pe;
386 char *path = NULL, *tmppath = NULL, *template = NULL;
387 FILE *orig = NULL, *tmp = NULL;
388 struct got_patch_hunk *h;
398 if (p->old == NULL && p->new == NULL)
399 return got_error(GOT_ERR_PATCH_MALFORMED);
401 err = got_worktree_resolve_path(&path, worktree,
402 p->new != NULL ? p->new : p->old);
405 err = got_pathlist_insert(&pe, &paths, path, NULL);
409 if (p->old != NULL && p->new == NULL) {
411 * special case: delete a file. don't try to match
412 * the lines but just schedule the removal.
414 err = got_worktree_schedule_delete(worktree, &paths,
415 0, NULL, delete_cb, delete_arg, repo, 0, 0);
417 } else if (p->old != NULL && strcmp(p->old, p->new)) {
418 err = got_error(GOT_ERR_PATCH_PATHS_DIFFER);
422 if (asprintf(&template, "%s/got-patch",
423 got_worktree_get_root_path(worktree)) == -1) {
424 err = got_error_from_errno(template);
428 err = got_opentemp_named(&tmppath, &tmp, template);
432 if (p->old == NULL) { /* create */
433 h = STAILQ_FIRST(&p->head);
434 if (h == NULL || STAILQ_NEXT(h, entries) != NULL) {
435 err = got_error(GOT_ERR_PATCH_MALFORMED);
438 for (i = 0; i < h->len; ++i) {
439 if (fprintf(tmp, "%s", h->lines[i]+1) < 0) {
440 err = got_error_from_errno("fprintf");
447 if ((orig = fopen(path, "r")) == NULL) {
448 err = got_error_from_errno2("fopen", path);
453 STAILQ_FOREACH(h, &p->head, entries) {
455 err = locate_hunk(orig, h, &lineno);
458 if ((pos = ftello(orig)) == -1) {
459 err = got_error_from_errno("ftello");
462 err = copy(tmp, orig, copypos, pos);
467 err = test_hunk(orig, h);
468 if (err != NULL && err->code == GOT_ERR_PATCH_DONT_APPLY) {
470 * try to apply the hunk again starting the search
471 * after the previous partial match.
473 if (fseek(orig, pos, SEEK_SET) == -1) {
474 err = got_error_from_errno("fseek");
477 linelen = getline(&line, &linesize, orig);
479 err = got_error_from_errno("getline");
488 err = apply_hunk(tmp, h, &lineno);
492 copypos = ftello(orig);
494 err = got_error_from_errno("ftello");
500 err = copy(tmp, orig, copypos, -1);
506 if (rename(tmppath, path) == -1) {
507 err = got_error_from_errno3("rename", tmppath, path);
512 err = got_worktree_schedule_add(worktree, &paths,
513 add_cb, add_arg, repo, 1);
515 printf("M %s\n", path); /* XXX */
518 if (err != NULL && p->old == NULL && path != NULL)
526 if (p->old == NULL && err != NULL)
532 got_pathlist_free(&paths);
536 const struct got_error *
537 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
538 got_worktree_delete_cb delete_cb, void *delete_arg,
539 got_worktree_checkout_cb add_cb, void *add_arg)
541 const struct got_error *err = NULL;
542 struct imsgbuf *ibuf;
543 int imsg_fds[2] = {-1, -1};
547 ibuf = calloc(1, sizeof(*ibuf));
549 err = got_error_from_errno("calloc");
553 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
554 err = got_error_from_errno("socketpair");
560 err = got_error_from_errno("fork");
562 } else if (pid == 0) {
563 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
568 if (close(imsg_fds[1]) == -1) {
569 err = got_error_from_errno("close");
573 imsg_init(ibuf, imsg_fds[0]);
575 err = send_patch(ibuf, fd);
580 while (!done && err == NULL) {
583 err = recv_patch(ibuf, &done, &p);
587 err = apply_patch(worktree, repo, &p, delete_cb, delete_arg,
595 if (fd != -1 && close(fd) == -1 && err == NULL)
596 err = got_error_from_errno("close");
599 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
600 err = got_error_from_errno("close");
601 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
602 err = got_error_from_errno("close");