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>
42 #include "got_error.h"
43 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_cancel.h"
47 #include "got_worktree.h"
48 #include "got_opentemp.h"
49 #include "got_patch.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_privsep.h"
55 #define MIN(a, b) ((a) < (b) ? (a) : (b))
57 struct got_patch_hunk {
58 STAILQ_ENTRY(got_patch_hunk) entries;
71 STAILQ_HEAD(, got_patch_hunk) head;
74 static const struct got_error *
75 send_patch(struct imsgbuf *ibuf, int fd)
77 const struct got_error *err = NULL;
79 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
81 err = got_error_from_errno(
82 "imsg_compose GOT_IMSG_PATCH_FILE");
87 if (imsg_flush(ibuf) == -1) {
88 err = got_error_from_errno("imsg_flush");
96 patch_free(struct got_patch *p)
98 struct got_patch_hunk *h;
101 while (!STAILQ_EMPTY(&p->head)) {
102 h = STAILQ_FIRST(&p->head);
103 STAILQ_REMOVE_HEAD(&p->head, entries);
105 for (i = 0; i < h->len; ++i)
115 static const struct got_error *
116 pushline(struct got_patch_hunk *h, const char *line)
121 if (h->len == h->cap) {
122 if ((newcap = h->cap * 1.5) == 0)
124 t = recallocarray(h->lines, h->cap, newcap,
125 sizeof(h->lines[0]));
127 return got_error_from_errno("recallocarray");
132 if ((t = strdup(line)) == NULL)
133 return got_error_from_errno("strdup");
135 h->lines[h->len++] = t;
139 static const struct got_error *
140 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
142 const struct got_error *err = NULL;
144 struct got_imsg_patch_hunk hdr;
145 struct got_imsg_patch patch;
146 struct got_patch_hunk *h = NULL;
149 memset(p, 0, sizeof(*p));
150 STAILQ_INIT(&p->head);
152 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
155 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
159 if (imsg.hdr.type != GOT_IMSG_PATCH) {
160 err = got_error(GOT_ERR_PRIVSEP_MSG);
163 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
164 if (datalen != sizeof(patch)) {
165 err = got_error(GOT_ERR_PRIVSEP_LEN);
168 memcpy(&patch, imsg.data, sizeof(patch));
169 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
170 err = got_error_from_errno("strdup");
173 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
174 err = got_error_from_errno("strdup");
177 if (p->old == NULL && p->new == NULL) {
178 err = got_error(GOT_ERR_PATCH_MALFORMED);
187 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
191 switch (imsg.hdr.type) {
192 case GOT_IMSG_PATCH_DONE:
194 case GOT_IMSG_PATCH_HUNK:
195 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
196 if (datalen != sizeof(hdr)) {
197 err = got_error(GOT_ERR_PRIVSEP_LEN);
200 memcpy(&hdr, imsg.data, sizeof(hdr));
201 if ((h = calloc(1, sizeof(*h))) == NULL) {
202 err = got_error_from_errno("calloc");
205 h->old_from = hdr.oldfrom;
206 h->old_lines = hdr.oldlines;
207 h->new_from = hdr.newfrom;
208 h->new_lines = hdr.newlines;
209 STAILQ_INSERT_TAIL(&p->head, h, entries);
211 case GOT_IMSG_PATCH_LINE:
213 err = got_error(GOT_ERR_PRIVSEP_MSG);
216 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
218 /* at least one char plus newline */
219 if (datalen < 2 || t[datalen-1] != '\0') {
220 err = got_error(GOT_ERR_PRIVSEP_MSG);
223 if (*t != ' ' && *t != '-' && *t != '+') {
224 err = got_error(GOT_ERR_PRIVSEP_MSG);
227 err = pushline(h, t);
232 err = got_error(GOT_ERR_PRIVSEP_MSG);
245 * Copy data from orig starting at copypos until pos into tmp.
246 * If pos is -1, copy until EOF.
248 static const struct got_error *
249 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
254 if (fseek(orig, copypos, SEEK_SET) == -1)
255 return got_error_from_errno("fseek");
257 while (pos == -1 || copypos < pos) {
260 len = MIN(len, (size_t)pos - copypos);
261 r = fread(buf, 1, len, orig);
262 if (r != len && ferror(orig))
263 return got_error_from_errno("fread");
264 w = fwrite(buf, 1, r, tmp);
266 return got_error_from_errno("fwrite");
268 if (r != len && feof(orig)) {
271 return got_error(GOT_ERR_PATCH_DONT_APPLY);
277 static const struct got_error *
278 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
280 const struct got_error *err = NULL;
282 char mode = *h->lines[0];
286 long match_lineno = -1;
289 linelen = getline(&line, &linesize, orig);
292 err = got_error_from_errno("getline");
293 else if (match == -1)
294 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
299 if ((mode == ' ' && !strcmp(h->lines[0]+1, line)) ||
300 (mode == '-' && !strcmp(h->lines[0]+1, line)) ||
301 (mode == '+' && *lineno == h->old_from)) {
302 match = ftello(orig);
304 err = got_error_from_errno("ftello");
308 match_lineno = (*lineno)-1;
311 if (*lineno >= h->old_from && match != -1)
317 *lineno = match_lineno;
318 if (fseek(orig, match, SEEK_SET) == -1)
319 err = got_error_from_errno("fseek");
326 static const struct got_error *
327 test_hunk(FILE *orig, struct got_patch_hunk *h)
329 const struct got_error *err = NULL;
331 size_t linesize = 0, i = 0;
334 for (i = 0; i < h->len; ++i) {
335 switch (*h->lines[i]) {
340 linelen = getline(&line, &linesize, orig);
343 err = got_error_from_errno("getline");
346 GOT_ERR_PATCH_DONT_APPLY);
349 if (strcmp(h->lines[i]+1, line)) {
350 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
362 static const struct got_error *
363 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
367 for (i = 0; i < h->len; ++i) {
368 switch (*h->lines[i]) {
370 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
371 return got_error_from_errno("fprintf");
377 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
378 return got_error_from_errno("fprintf");
385 static const struct got_error *
386 patch_file(struct got_patch *p, const char *path, FILE *tmp)
388 const struct got_error *err = NULL;
389 struct got_patch_hunk *h;
398 if (p->old == NULL) { /* create */
399 h = STAILQ_FIRST(&p->head);
400 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
401 return got_error(GOT_ERR_PATCH_MALFORMED);
402 for (i = 0; i < h->len; ++i) {
403 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
404 return got_error_from_errno("fprintf");
409 if ((orig = fopen(path, "r")) == NULL) {
410 err = got_error_from_errno2("fopen", path);
415 STAILQ_FOREACH(h, &p->head, entries) {
416 if (h->lines == NULL)
420 err = locate_hunk(orig, h, &pos, &lineno);
423 err = copy(tmp, orig, copypos, pos);
428 err = test_hunk(orig, h);
429 if (err != NULL && err->code == GOT_ERR_PATCH_DONT_APPLY) {
431 * try to apply the hunk again starting the search
432 * after the previous partial match.
434 if (fseek(orig, pos, SEEK_SET) == -1) {
435 err = got_error_from_errno("fseek");
438 linelen = getline(&line, &linesize, orig);
440 err = got_error_from_errno("getline");
449 err = apply_hunk(tmp, h, &lineno);
453 copypos = ftello(orig);
455 err = got_error_from_errno("ftello");
461 if (p->new == NULL) {
464 if (fstat(fileno(orig), &sb) == -1)
465 err = got_error_from_errno("fstat");
466 else if (sb.st_size != copypos)
467 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
468 } else if (!feof(orig))
469 err = copy(tmp, orig, copypos, -1);
477 static const struct got_error *
478 build_pathlist(const char *p, char **path, struct got_pathlist_head *head,
479 struct got_worktree *worktree)
481 const struct got_error *err;
482 struct got_pathlist_entry *pe;
484 err = got_worktree_resolve_path(path, worktree, p);
486 err = got_pathlist_insert(&pe, head, *path, NULL);
490 static const struct got_error *
491 can_rm(void *arg, unsigned char status, unsigned char staged_status,
492 const char *path, struct got_object_id *blob_id,
493 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
494 int dirfd, const char *de_name)
496 if (status == GOT_STATUS_NONEXISTENT)
497 return got_error_set_errno(ENOENT, path);
498 if (status != GOT_STATUS_NO_CHANGE &&
499 status != GOT_STATUS_ADD &&
500 status != GOT_STATUS_MODIFY &&
501 status != GOT_STATUS_MODE_CHANGE)
502 return got_error_path(path, GOT_ERR_FILE_STATUS);
503 if (staged_status == GOT_STATUS_DELETE)
504 return got_error_path(path, GOT_ERR_FILE_STATUS);
508 static const struct got_error *
509 can_add(void *arg, unsigned char status, unsigned char staged_status,
510 const char *path, struct got_object_id *blob_id,
511 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
512 int dirfd, const char *de_name)
514 if (status != GOT_STATUS_NONEXISTENT)
515 return got_error_path(path, GOT_ERR_FILE_STATUS);
519 static const struct got_error *
520 can_edit(void *arg, unsigned char status, unsigned char staged_status,
521 const char *path, struct got_object_id *blob_id,
522 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
523 int dirfd, const char *de_name)
525 if (status == GOT_STATUS_NONEXISTENT)
526 return got_error_set_errno(ENOENT, path);
527 if (status != GOT_STATUS_NO_CHANGE &&
528 status != GOT_STATUS_ADD &&
529 status != GOT_STATUS_MODIFY)
530 return got_error_path(path, GOT_ERR_FILE_STATUS);
531 if (staged_status == GOT_STATUS_DELETE)
532 return got_error_path(path, GOT_ERR_FILE_STATUS);
536 static const struct got_error *
537 check_file_status(struct got_patch *p, int file_renamed,
538 struct got_worktree *worktree, struct got_repository *repo,
539 struct got_pathlist_head *old, struct got_pathlist_head *new,
540 got_cancel_cb cancel_cb, void *cancel_arg)
542 static const struct got_error *err;
544 if (p->old != NULL && p->new == NULL)
545 return got_worktree_status(worktree, old, repo, 0,
546 can_rm, NULL, cancel_cb, cancel_arg);
547 else if (file_renamed) {
548 err = got_worktree_status(worktree, old, repo, 0,
549 can_rm, NULL, cancel_cb, cancel_arg);
552 return got_worktree_status(worktree, new, repo, 0,
553 can_add, NULL, cancel_cb, cancel_arg);
554 } else if (p->old == NULL)
555 return got_worktree_status(worktree, new, repo, 0,
556 can_add, NULL, cancel_cb, cancel_arg);
558 return got_worktree_status(worktree, new, repo, 0,
559 can_edit, NULL, cancel_cb, cancel_arg);
562 static const struct got_error *
563 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
564 struct got_patch *p, got_worktree_delete_cb delete_cb, void *delete_arg,
565 got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
568 const struct got_error *err = NULL;
569 struct got_pathlist_head oldpaths, newpaths;
570 int file_renamed = 0;
571 char *oldpath = NULL, *newpath = NULL;
572 char *tmppath = NULL, *template = NULL;
575 TAILQ_INIT(&oldpaths);
576 TAILQ_INIT(&newpaths);
578 err = build_pathlist(p->old != NULL ? p->old : p->new, &oldpath,
579 &oldpaths, worktree);
583 err = build_pathlist(p->new != NULL ? p->new : p->old, &newpath,
584 &newpaths, worktree);
588 if (p->old != NULL && p->new != NULL && strcmp(p->old, p->new))
591 err = check_file_status(p, file_renamed, worktree, repo, &oldpaths,
592 &newpaths, cancel_cb, cancel_arg);
596 if (asprintf(&template, "%s/got-patch",
597 got_worktree_get_root_path(worktree)) == -1) {
598 err = got_error_from_errno(template);
602 err = got_opentemp_named(&tmppath, &tmp, template);
605 err = patch_file(p, oldpath, tmp);
609 if (p->old != NULL && p->new == NULL) {
610 err = got_worktree_schedule_delete(worktree, &oldpaths,
611 0, NULL, delete_cb, delete_arg, repo, 0, 0);
615 if (rename(tmppath, newpath) == -1) {
616 err = got_error_from_errno3("rename", tmppath, newpath);
621 err = got_worktree_schedule_delete(worktree, &oldpaths,
622 0, NULL, delete_cb, delete_arg, repo, 0, 0);
624 err = got_worktree_schedule_add(worktree, &newpaths,
625 add_cb, add_arg, repo, 1);
626 } else if (p->old == NULL)
627 err = got_worktree_schedule_add(worktree, &newpaths,
628 add_cb, add_arg, repo, 1);
630 printf("M %s\n", oldpath); /* XXX */
633 if (err != NULL && newpath != NULL && (file_renamed || p->old == NULL))
639 got_pathlist_free(&oldpaths);
640 got_pathlist_free(&newpaths);
646 const struct got_error *
647 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
648 got_worktree_delete_cb delete_cb, void *delete_arg,
649 got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
652 const struct got_error *err = NULL;
653 struct imsgbuf *ibuf;
654 int imsg_fds[2] = {-1, -1};
658 ibuf = calloc(1, sizeof(*ibuf));
660 err = got_error_from_errno("calloc");
664 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
665 err = got_error_from_errno("socketpair");
671 err = got_error_from_errno("fork");
673 } else if (pid == 0) {
674 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
679 if (close(imsg_fds[1]) == -1) {
680 err = got_error_from_errno("close");
684 imsg_init(ibuf, imsg_fds[0]);
686 err = send_patch(ibuf, fd);
691 while (!done && err == NULL) {
694 err = recv_patch(ibuf, &done, &p);
698 err = apply_patch(worktree, repo, &p, delete_cb, delete_arg,
699 add_cb, add_arg, cancel_cb, cancel_arg);
706 if (fd != -1 && close(fd) == -1 && err == NULL)
707 err = got_error_from_errno("close");
710 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
711 err = got_error_from_errno("close");
712 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
713 err = got_error_from_errno("close");