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;
72 STAILQ_HEAD(, got_patch_hunk) head;
75 static const struct got_error *
76 send_patch(struct imsgbuf *ibuf, int fd)
78 const struct got_error *err = NULL;
80 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
82 err = got_error_from_errno(
83 "imsg_compose GOT_IMSG_PATCH_FILE");
88 if (imsg_flush(ibuf) == -1) {
89 err = got_error_from_errno("imsg_flush");
97 patch_free(struct got_patch *p)
99 struct got_patch_hunk *h;
102 while (!STAILQ_EMPTY(&p->head)) {
103 h = STAILQ_FIRST(&p->head);
104 STAILQ_REMOVE_HEAD(&p->head, entries);
106 for (i = 0; i < h->len; ++i)
116 static const struct got_error *
117 pushline(struct got_patch_hunk *h, const char *line)
122 if (h->len == h->cap) {
123 if ((newcap = h->cap * 1.5) == 0)
125 t = recallocarray(h->lines, h->cap, newcap,
126 sizeof(h->lines[0]));
128 return got_error_from_errno("recallocarray");
133 if ((t = strdup(line)) == NULL)
134 return got_error_from_errno("strdup");
136 h->lines[h->len++] = t;
140 static const struct got_error *
141 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
143 const struct got_error *err = NULL;
145 struct got_imsg_patch_hunk hdr;
146 struct got_imsg_patch patch;
147 struct got_patch_hunk *h = NULL;
150 memset(p, 0, sizeof(*p));
151 STAILQ_INIT(&p->head);
153 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
156 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
160 if (imsg.hdr.type != GOT_IMSG_PATCH) {
161 err = got_error(GOT_ERR_PRIVSEP_MSG);
164 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
165 if (datalen != sizeof(patch)) {
166 err = got_error(GOT_ERR_PRIVSEP_LEN);
169 memcpy(&patch, imsg.data, sizeof(patch));
170 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
171 err = got_error_from_errno("strdup");
174 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
175 err = got_error_from_errno("strdup");
178 if (p->old == NULL && p->new == NULL) {
179 err = got_error(GOT_ERR_PATCH_MALFORMED);
188 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
192 switch (imsg.hdr.type) {
193 case GOT_IMSG_PATCH_DONE:
195 case GOT_IMSG_PATCH_HUNK:
196 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
197 if (datalen != sizeof(hdr)) {
198 err = got_error(GOT_ERR_PRIVSEP_LEN);
201 memcpy(&hdr, imsg.data, sizeof(hdr));
202 if ((h = calloc(1, sizeof(*h))) == NULL) {
203 err = got_error_from_errno("calloc");
206 h->old_from = hdr.oldfrom;
207 h->old_lines = hdr.oldlines;
208 h->new_from = hdr.newfrom;
209 h->new_lines = hdr.newlines;
210 STAILQ_INSERT_TAIL(&p->head, h, entries);
212 case GOT_IMSG_PATCH_LINE:
214 err = got_error(GOT_ERR_PRIVSEP_MSG);
217 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
219 /* at least one char plus newline */
220 if (datalen < 2 || t[datalen-1] != '\0') {
221 err = got_error(GOT_ERR_PRIVSEP_MSG);
224 if (*t != ' ' && *t != '-' && *t != '+') {
225 err = got_error(GOT_ERR_PRIVSEP_MSG);
228 err = pushline(h, t);
233 err = got_error(GOT_ERR_PRIVSEP_MSG);
246 * Copy data from orig starting at copypos until pos into tmp.
247 * If pos is -1, copy until EOF.
249 static const struct got_error *
250 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
255 if (fseek(orig, copypos, SEEK_SET) == -1)
256 return got_error_from_errno("fseek");
258 while (pos == -1 || copypos < pos) {
261 len = MIN(len, (size_t)pos - copypos);
262 r = fread(buf, 1, len, orig);
263 if (r != len && ferror(orig))
264 return got_error_from_errno("fread");
265 w = fwrite(buf, 1, r, tmp);
267 return got_error_from_errno("fwrite");
269 if (r != len && feof(orig)) {
272 return got_error(GOT_ERR_PATCH_DONT_APPLY);
278 static const struct got_error *
279 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
281 const struct got_error *err = NULL;
283 char mode = *h->lines[0];
287 long match_lineno = -1;
290 linelen = getline(&line, &linesize, orig);
293 err = got_error_from_errno("getline");
294 else if (match == -1)
295 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
300 if ((mode == ' ' && !strcmp(h->lines[0]+1, line)) ||
301 (mode == '-' && !strcmp(h->lines[0]+1, line)) ||
302 (mode == '+' && *lineno == h->old_from)) {
303 match = ftello(orig);
305 err = got_error_from_errno("ftello");
309 match_lineno = (*lineno)-1;
312 if (*lineno >= h->old_from && match != -1)
318 *lineno = match_lineno;
319 if (fseek(orig, match, SEEK_SET) == -1)
320 err = got_error_from_errno("fseek");
327 static const struct got_error *
328 test_hunk(FILE *orig, struct got_patch_hunk *h)
330 const struct got_error *err = NULL;
332 size_t linesize = 0, i = 0;
335 for (i = 0; i < h->len; ++i) {
336 switch (*h->lines[i]) {
341 linelen = getline(&line, &linesize, orig);
344 err = got_error_from_errno("getline");
347 GOT_ERR_PATCH_DONT_APPLY);
350 if (strcmp(h->lines[i]+1, line)) {
351 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
363 static const struct got_error *
364 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
368 for (i = 0; i < h->len; ++i) {
369 switch (*h->lines[i]) {
371 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
372 return got_error_from_errno("fprintf");
378 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
379 return got_error_from_errno("fprintf");
386 static const struct got_error *
387 patch_file(struct got_patch *p, const char *path, FILE *tmp)
389 const struct got_error *err = NULL;
390 struct got_patch_hunk *h;
399 if (p->old == NULL) { /* create */
400 h = STAILQ_FIRST(&p->head);
401 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
402 return got_error(GOT_ERR_PATCH_MALFORMED);
405 for (i = 0; i < h->len; ++i) {
406 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
407 return got_error_from_errno("fprintf");
412 if ((orig = fopen(path, "r")) == NULL) {
413 err = got_error_from_errno2("fopen", path);
418 STAILQ_FOREACH(h, &p->head, entries) {
419 if (h->lines == NULL)
423 err = locate_hunk(orig, h, &pos, &lineno);
427 err = copy(tmp, orig, copypos, pos);
432 err = test_hunk(orig, h);
433 if (err != NULL && err->code == GOT_ERR_PATCH_DONT_APPLY) {
435 * try to apply the hunk again starting the search
436 * after the previous partial match.
438 if (fseek(orig, pos, SEEK_SET) == -1) {
439 err = got_error_from_errno("fseek");
442 linelen = getline(&line, &linesize, orig);
444 err = got_error_from_errno("getline");
454 err = apply_hunk(tmp, h, &lineno);
458 copypos = ftello(orig);
460 err = got_error_from_errno("ftello");
466 if (p->new == NULL) {
469 if (fstat(fileno(orig), &sb) == -1)
470 err = got_error_from_errno("fstat");
471 else if (sb.st_size != copypos)
472 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
473 } else if (!p->nop && !feof(orig))
474 err = copy(tmp, orig, copypos, -1);
482 static const struct got_error *
483 build_pathlist(const char *p, char **path, struct got_pathlist_head *head,
484 struct got_worktree *worktree)
486 const struct got_error *err;
487 struct got_pathlist_entry *pe;
489 err = got_worktree_resolve_path(path, worktree, p);
491 err = got_pathlist_insert(&pe, head, *path, NULL);
495 static const struct got_error *
496 can_rm(void *arg, unsigned char status, unsigned char staged_status,
497 const char *path, struct got_object_id *blob_id,
498 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
499 int dirfd, const char *de_name)
501 if (status == GOT_STATUS_NONEXISTENT)
502 return got_error_set_errno(ENOENT, path);
503 if (status != GOT_STATUS_NO_CHANGE &&
504 status != GOT_STATUS_ADD &&
505 status != GOT_STATUS_MODIFY &&
506 status != GOT_STATUS_MODE_CHANGE)
507 return got_error_path(path, GOT_ERR_FILE_STATUS);
508 if (staged_status == GOT_STATUS_DELETE)
509 return got_error_path(path, GOT_ERR_FILE_STATUS);
513 static const struct got_error *
514 can_add(void *arg, unsigned char status, unsigned char staged_status,
515 const char *path, struct got_object_id *blob_id,
516 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
517 int dirfd, const char *de_name)
519 if (status != GOT_STATUS_NONEXISTENT)
520 return got_error_path(path, GOT_ERR_FILE_STATUS);
524 static const struct got_error *
525 can_edit(void *arg, unsigned char status, unsigned char staged_status,
526 const char *path, struct got_object_id *blob_id,
527 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
528 int dirfd, const char *de_name)
530 if (status == GOT_STATUS_NONEXISTENT)
531 return got_error_set_errno(ENOENT, path);
532 if (status != GOT_STATUS_NO_CHANGE &&
533 status != GOT_STATUS_ADD &&
534 status != GOT_STATUS_MODIFY)
535 return got_error_path(path, GOT_ERR_FILE_STATUS);
536 if (staged_status == GOT_STATUS_DELETE)
537 return got_error_path(path, GOT_ERR_FILE_STATUS);
541 static const struct got_error *
542 check_file_status(struct got_patch *p, int file_renamed,
543 struct got_worktree *worktree, struct got_repository *repo,
544 struct got_pathlist_head *old, struct got_pathlist_head *new,
545 got_cancel_cb cancel_cb, void *cancel_arg)
547 static const struct got_error *err;
549 if (p->old != NULL && p->new == NULL)
550 return got_worktree_status(worktree, old, repo, 0,
551 can_rm, NULL, cancel_cb, cancel_arg);
552 else if (file_renamed) {
553 err = got_worktree_status(worktree, old, repo, 0,
554 can_rm, NULL, cancel_cb, cancel_arg);
557 return got_worktree_status(worktree, new, repo, 0,
558 can_add, NULL, cancel_cb, cancel_arg);
559 } else if (p->old == NULL)
560 return got_worktree_status(worktree, new, repo, 0,
561 can_add, NULL, cancel_cb, cancel_arg);
563 return got_worktree_status(worktree, new, repo, 0,
564 can_edit, NULL, cancel_cb, cancel_arg);
567 static const struct got_error *
568 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
569 struct got_patch *p, got_worktree_delete_cb delete_cb, void *delete_arg,
570 got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
573 const struct got_error *err = NULL;
574 struct got_pathlist_head oldpaths, newpaths;
575 int file_renamed = 0;
576 char *oldpath = NULL, *newpath = NULL;
577 char *tmppath = NULL, *template = NULL;
580 TAILQ_INIT(&oldpaths);
581 TAILQ_INIT(&newpaths);
583 err = build_pathlist(p->old != NULL ? p->old : p->new, &oldpath,
584 &oldpaths, worktree);
588 err = build_pathlist(p->new != NULL ? p->new : p->old, &newpath,
589 &newpaths, worktree);
593 if (p->old != NULL && p->new != NULL && strcmp(p->old, p->new))
596 err = check_file_status(p, file_renamed, worktree, repo, &oldpaths,
597 &newpaths, cancel_cb, cancel_arg);
601 if (asprintf(&template, "%s/got-patch",
602 got_worktree_get_root_path(worktree)) == -1) {
603 err = got_error_from_errno(template);
608 err = got_opentemp_named(&tmppath, &tmp, template);
611 err = patch_file(p, oldpath, tmp);
618 if (p->old != NULL && p->new == NULL) {
619 err = got_worktree_schedule_delete(worktree, &oldpaths,
620 0, NULL, delete_cb, delete_arg, repo, 0, 0);
624 if (rename(tmppath, newpath) == -1) {
625 err = got_error_from_errno3("rename", tmppath, newpath);
630 err = got_worktree_schedule_delete(worktree, &oldpaths,
631 0, NULL, delete_cb, delete_arg, repo, 0, 0);
633 err = got_worktree_schedule_add(worktree, &newpaths,
634 add_cb, add_arg, repo, 1);
635 } else if (p->old == NULL)
636 err = got_worktree_schedule_add(worktree, &newpaths,
637 add_cb, add_arg, repo, 1);
639 printf("M %s\n", oldpath); /* XXX */
642 if (err != NULL && newpath != NULL && (file_renamed || p->old == NULL))
648 got_pathlist_free(&oldpaths);
649 got_pathlist_free(&newpaths);
655 const struct got_error *
656 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
657 int nop, got_worktree_delete_cb delete_cb, void *delete_arg,
658 got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
661 const struct got_error *err = NULL;
662 struct imsgbuf *ibuf;
663 int imsg_fds[2] = {-1, -1};
667 ibuf = calloc(1, sizeof(*ibuf));
669 err = got_error_from_errno("calloc");
673 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
674 err = got_error_from_errno("socketpair");
680 err = got_error_from_errno("fork");
682 } else if (pid == 0) {
683 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
688 if (close(imsg_fds[1]) == -1) {
689 err = got_error_from_errno("close");
693 imsg_init(ibuf, imsg_fds[0]);
695 err = send_patch(ibuf, fd);
700 while (!done && err == NULL) {
703 err = recv_patch(ibuf, &done, &p);
708 err = apply_patch(worktree, repo, &p, delete_cb, delete_arg,
709 add_cb, add_arg, cancel_cb, cancel_arg);
716 if (fd != -1 && close(fd) == -1 && err == NULL)
717 err = got_error_from_errno("close");
720 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
721 err = got_error_from_errno("close");
722 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
723 err = got_error_from_errno("close");