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");
175 if (p->old == NULL && p->new == NULL) {
176 err = got_error(GOT_ERR_PATCH_MALFORMED);
185 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
189 switch (imsg.hdr.type) {
190 case GOT_IMSG_PATCH_DONE:
192 case GOT_IMSG_PATCH_HUNK:
193 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
194 if (datalen != sizeof(hdr)) {
195 err = got_error(GOT_ERR_PRIVSEP_LEN);
198 memcpy(&hdr, imsg.data, sizeof(hdr));
199 if ((h = calloc(1, sizeof(*h))) == NULL) {
200 err = got_error_from_errno("calloc");
203 h->old_from = hdr.oldfrom;
204 h->old_lines = hdr.oldlines;
205 h->new_from = hdr.newfrom;
206 h->new_lines = hdr.newlines;
207 STAILQ_INSERT_TAIL(&p->head, h, entries);
209 case GOT_IMSG_PATCH_LINE:
211 err = got_error(GOT_ERR_PRIVSEP_MSG);
214 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
216 /* at least one char plus newline */
217 if (datalen < 2 || t[datalen-1] != '\0') {
218 err = got_error(GOT_ERR_PRIVSEP_MSG);
221 if (*t != ' ' && *t != '-' && *t != '+') {
222 err = got_error(GOT_ERR_PRIVSEP_MSG);
225 err = pushline(h, t);
230 err = got_error(GOT_ERR_PRIVSEP_MSG);
243 * Copy data from orig starting at copypos until pos into tmp.
244 * If pos is -1, copy until EOF.
246 static const struct got_error *
247 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
252 if (fseek(orig, copypos, SEEK_SET) == -1)
253 return got_error_from_errno("fseek");
255 while (pos == -1 || copypos < pos) {
258 len = MIN(len, (size_t)pos - copypos);
259 r = fread(buf, 1, len, orig);
260 if (r != len && ferror(orig))
261 return got_error_from_errno("fread");
262 w = fwrite(buf, 1, r, tmp);
264 return got_error_from_errno("fwrite");
266 if (r != len && feof(orig)) {
269 return got_error(GOT_ERR_PATCH_DONT_APPLY);
275 static const struct got_error *
276 locate_hunk(FILE *orig, struct got_patch_hunk *h, long *lineno)
278 const struct got_error *err = NULL;
280 char mode = *h->lines[0];
284 long match_lineno = -1;
287 linelen = getline(&line, &linesize, orig);
290 err = got_error_from_errno("getline");
291 else if (match == -1)
292 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
297 if ((mode == ' ' && !strcmp(h->lines[0]+1, line)) ||
298 (mode == '-' && !strcmp(h->lines[0]+1, line)) ||
299 (mode == '+' && *lineno == h->old_from)) {
300 match = ftello(orig);
302 err = got_error_from_errno("ftello");
306 match_lineno = (*lineno)-1;
309 if (*lineno >= h->old_from && match != -1)
314 *lineno = match_lineno;
315 if (fseek(orig, match, SEEK_SET) == -1)
316 err = got_error_from_errno("fseek");
323 static const struct got_error *
324 test_hunk(FILE *orig, struct got_patch_hunk *h)
326 const struct got_error *err = NULL;
328 size_t linesize = 0, i = 0;
331 for (i = 0; i < h->len; ++i) {
332 switch (*h->lines[i]) {
337 linelen = getline(&line, &linesize, orig);
340 err = got_error_from_errno("getline");
343 GOT_ERR_PATCH_DONT_APPLY);
346 if (strcmp(h->lines[i]+1, line)) {
347 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
359 static const struct got_error *
360 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
364 for (i = 0; i < h->len; ++i) {
365 switch (*h->lines[i]) {
367 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
368 return got_error_from_errno("fprintf");
374 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
375 return got_error_from_errno("fprintf");
382 static const struct got_error *
383 patch_file(struct got_patch *p, const char *path, FILE *tmp)
385 const struct got_error *err = NULL;
386 struct got_patch_hunk *h;
395 if (p->old == NULL) { /* create */
396 h = STAILQ_FIRST(&p->head);
397 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
398 return got_error(GOT_ERR_PATCH_MALFORMED);
399 for (i = 0; i < h->len; ++i) {
400 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
401 return got_error_from_errno("fprintf");
406 if ((orig = fopen(path, "r")) == NULL) {
407 err = got_error_from_errno2("fopen", path);
412 STAILQ_FOREACH(h, &p->head, entries) {
413 if (h->lines == NULL)
417 err = locate_hunk(orig, h, &lineno);
420 if ((pos = ftello(orig)) == -1) {
421 err = got_error_from_errno("ftello");
424 err = copy(tmp, orig, copypos, pos);
429 err = test_hunk(orig, h);
430 if (err != NULL && err->code == GOT_ERR_PATCH_DONT_APPLY) {
432 * try to apply the hunk again starting the search
433 * after the previous partial match.
435 if (fseek(orig, pos, SEEK_SET) == -1) {
436 err = got_error_from_errno("fseek");
439 linelen = getline(&line, &linesize, orig);
441 err = got_error_from_errno("getline");
450 err = apply_hunk(tmp, h, &lineno);
454 copypos = ftello(orig);
456 err = got_error_from_errno("ftello");
462 err = copy(tmp, orig, copypos, -1);
470 static const struct got_error *
471 build_pathlist(const char *p, char **path, struct got_pathlist_head *head,
472 struct got_worktree *worktree)
474 const struct got_error *err;
475 struct got_pathlist_entry *pe;
477 err = got_worktree_resolve_path(path, worktree, p);
479 err = got_pathlist_insert(&pe, head, *path, NULL);
483 static const struct got_error *
484 can_rm(void *arg, unsigned char status, unsigned char staged_status,
485 const char *path, struct got_object_id *blob_id,
486 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
487 int dirfd, const char *de_name)
489 if (status == GOT_STATUS_NONEXISTENT)
490 return got_error_set_errno(ENOENT, path);
491 if (status != GOT_STATUS_NO_CHANGE &&
492 status != GOT_STATUS_ADD &&
493 status != GOT_STATUS_MODIFY &&
494 status != GOT_STATUS_MODE_CHANGE)
495 return got_error_path(path, GOT_ERR_FILE_STATUS);
496 if (staged_status == GOT_STATUS_DELETE)
497 return got_error_path(path, GOT_ERR_FILE_STATUS);
501 static const struct got_error *
502 can_add(void *arg, unsigned char status, unsigned char staged_status,
503 const char *path, struct got_object_id *blob_id,
504 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
505 int dirfd, const char *de_name)
507 if (status != GOT_STATUS_NONEXISTENT)
508 return got_error_path(path, GOT_ERR_FILE_STATUS);
512 static const struct got_error *
513 can_edit(void *arg, unsigned char status, unsigned char staged_status,
514 const char *path, struct got_object_id *blob_id,
515 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
516 int dirfd, const char *de_name)
518 if (status == GOT_STATUS_NONEXISTENT)
519 return got_error_set_errno(ENOENT, path);
520 if (status != GOT_STATUS_NO_CHANGE &&
521 status != GOT_STATUS_ADD &&
522 status != GOT_STATUS_MODIFY)
523 return got_error_path(path, GOT_ERR_FILE_STATUS);
524 if (staged_status == GOT_STATUS_DELETE)
525 return got_error_path(path, GOT_ERR_FILE_STATUS);
529 static const struct got_error *
530 check_file_status(struct got_patch *p, int file_renamed,
531 struct got_worktree *worktree, struct got_repository *repo,
532 struct got_pathlist_head *old, struct got_pathlist_head *new,
533 got_cancel_cb cancel_cb, void *cancel_arg)
535 static const struct got_error *err;
537 if (p->old != NULL && p->new == NULL)
538 return got_worktree_status(worktree, old, repo, 0,
539 can_rm, NULL, cancel_cb, cancel_arg);
540 else if (file_renamed) {
541 err = got_worktree_status(worktree, old, repo, 0,
542 can_rm, NULL, cancel_cb, cancel_arg);
545 return got_worktree_status(worktree, new, repo, 0,
546 can_add, NULL, cancel_cb, cancel_arg);
547 } else if (p->old == NULL)
548 return got_worktree_status(worktree, new, repo, 0,
549 can_add, NULL, cancel_cb, cancel_arg);
551 return got_worktree_status(worktree, new, repo, 0,
552 can_edit, NULL, cancel_cb, cancel_arg);
555 static const struct got_error *
556 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
557 struct got_patch *p, got_worktree_delete_cb delete_cb, void *delete_arg,
558 got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
561 const struct got_error *err = NULL;
562 struct got_pathlist_head oldpaths, newpaths;
563 int file_renamed = 0;
564 char *oldpath = NULL, *newpath = NULL;
565 char *tmppath = NULL, *template = NULL;
568 TAILQ_INIT(&oldpaths);
569 TAILQ_INIT(&newpaths);
571 err = build_pathlist(p->old != NULL ? p->old : p->new, &oldpath,
572 &oldpaths, worktree);
576 err = build_pathlist(p->new != NULL ? p->new : p->old, &newpath,
577 &newpaths, worktree);
581 if (p->old != NULL && p->new != NULL && strcmp(p->old, p->new))
584 err = check_file_status(p, file_renamed, worktree, repo, &oldpaths,
585 &newpaths, cancel_cb, cancel_arg);
589 if (p->old != NULL && p->new == NULL) {
591 * special case: delete a file. don't try to match
592 * the lines but just schedule the removal.
594 err = got_worktree_schedule_delete(worktree, &oldpaths,
595 0, NULL, delete_cb, delete_arg, repo, 0, 0);
599 if (asprintf(&template, "%s/got-patch",
600 got_worktree_get_root_path(worktree)) == -1) {
601 err = got_error_from_errno(template);
605 err = got_opentemp_named(&tmppath, &tmp, template);
608 err = patch_file(p, oldpath, tmp);
612 if (rename(tmppath, newpath) == -1) {
613 err = got_error_from_errno3("rename", tmppath, newpath);
618 err = got_worktree_schedule_delete(worktree, &oldpaths,
619 0, NULL, delete_cb, delete_arg, repo, 0, 0);
621 err = got_worktree_schedule_add(worktree, &newpaths,
622 add_cb, add_arg, repo, 1);
623 } else if (p->old == NULL)
624 err = got_worktree_schedule_add(worktree, &newpaths,
625 add_cb, add_arg, repo, 1);
627 printf("M %s\n", oldpath); /* XXX */
630 if (err != NULL && newpath != NULL && (file_renamed || p->old == NULL))
636 got_pathlist_free(&oldpaths);
637 got_pathlist_free(&newpaths);
643 const struct got_error *
644 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
645 got_worktree_delete_cb delete_cb, void *delete_arg,
646 got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
649 const struct got_error *err = NULL;
650 struct imsgbuf *ibuf;
651 int imsg_fds[2] = {-1, -1};
655 ibuf = calloc(1, sizeof(*ibuf));
657 err = got_error_from_errno("calloc");
661 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
662 err = got_error_from_errno("socketpair");
668 err = got_error_from_errno("fork");
670 } else if (pid == 0) {
671 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
676 if (close(imsg_fds[1]) == -1) {
677 err = got_error_from_errno("close");
681 imsg_init(ibuf, imsg_fds[0]);
683 err = send_patch(ibuf, fd);
688 while (!done && err == NULL) {
691 err = recv_patch(ibuf, &done, &p);
695 err = apply_patch(worktree, repo, &p, delete_cb, delete_arg,
696 add_cb, add_arg, cancel_cb, cancel_arg);
703 if (fd != -1 && close(fd) == -1 && err == NULL)
704 err = got_error_from_errno("close");
707 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
708 err = got_error_from_errno("close");
709 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
710 err = got_error_from_errno("close");