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>
41 #include "got_error.h"
42 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
47 #include "got_opentemp.h"
48 #include "got_patch.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_privsep.h"
54 #define MIN(a, b) ((a) < (b) ? (a) : (b))
56 struct got_patch_hunk {
57 STAILQ_ENTRY(got_patch_hunk) entries;
58 const struct got_error *err;
69 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
73 struct got_patch_hunk_head head;
77 got_patch_progress_cb progress_cb;
79 struct got_patch_hunk_head *head;
82 static const struct got_error *
83 send_patch(struct imsgbuf *ibuf, int fd)
85 const struct got_error *err = NULL;
87 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
89 err = got_error_from_errno(
90 "imsg_compose GOT_IMSG_PATCH_FILE");
95 if (imsg_flush(ibuf) == -1) {
96 err = got_error_from_errno("imsg_flush");
104 patch_free(struct got_patch *p)
106 struct got_patch_hunk *h;
109 while (!STAILQ_EMPTY(&p->head)) {
110 h = STAILQ_FIRST(&p->head);
111 STAILQ_REMOVE_HEAD(&p->head, entries);
113 for (i = 0; i < h->len; ++i)
123 static const struct got_error *
124 pushline(struct got_patch_hunk *h, const char *line)
129 if (h->len == h->cap) {
130 if ((newcap = h->cap * 1.5) == 0)
132 t = recallocarray(h->lines, h->cap, newcap,
133 sizeof(h->lines[0]));
135 return got_error_from_errno("recallocarray");
140 if ((t = strdup(line)) == NULL)
141 return got_error_from_errno("strdup");
143 h->lines[h->len++] = t;
147 static const struct got_error *
148 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
150 const struct got_error *err = NULL;
152 struct got_imsg_patch_hunk hdr;
153 struct got_imsg_patch patch;
154 struct got_patch_hunk *h = NULL;
157 memset(p, 0, sizeof(*p));
158 STAILQ_INIT(&p->head);
160 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
163 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
167 if (imsg.hdr.type != GOT_IMSG_PATCH) {
168 err = got_error(GOT_ERR_PRIVSEP_MSG);
171 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
172 if (datalen != sizeof(patch)) {
173 err = got_error(GOT_ERR_PRIVSEP_LEN);
176 memcpy(&patch, imsg.data, sizeof(patch));
177 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
178 err = got_error_from_errno("strdup");
181 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
182 err = got_error_from_errno("strdup");
185 if (p->old == NULL && p->new == NULL) {
186 err = got_error(GOT_ERR_PATCH_MALFORMED);
195 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
199 switch (imsg.hdr.type) {
200 case GOT_IMSG_PATCH_DONE:
202 case GOT_IMSG_PATCH_HUNK:
203 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
204 if (datalen != sizeof(hdr)) {
205 err = got_error(GOT_ERR_PRIVSEP_LEN);
208 memcpy(&hdr, imsg.data, sizeof(hdr));
209 if ((h = calloc(1, sizeof(*h))) == NULL) {
210 err = got_error_from_errno("calloc");
213 h->old_from = hdr.oldfrom;
214 h->old_lines = hdr.oldlines;
215 h->new_from = hdr.newfrom;
216 h->new_lines = hdr.newlines;
217 STAILQ_INSERT_TAIL(&p->head, h, entries);
219 case GOT_IMSG_PATCH_LINE:
221 err = got_error(GOT_ERR_PRIVSEP_MSG);
224 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
226 /* at least one char plus newline */
227 if (datalen < 2 || t[datalen-1] != '\0') {
228 err = got_error(GOT_ERR_PRIVSEP_MSG);
231 if (*t != ' ' && *t != '-' && *t != '+') {
232 err = got_error(GOT_ERR_PRIVSEP_MSG);
235 err = pushline(h, t);
240 err = got_error(GOT_ERR_PRIVSEP_MSG);
253 * Copy data from orig starting at copypos until pos into tmp.
254 * If pos is -1, copy until EOF.
256 static const struct got_error *
257 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
262 if (fseek(orig, copypos, SEEK_SET) == -1)
263 return got_error_from_errno("fseek");
265 while (pos == -1 || copypos < pos) {
268 len = MIN(len, (size_t)pos - copypos);
269 r = fread(buf, 1, len, orig);
270 if (r != len && ferror(orig))
271 return got_error_from_errno("fread");
272 w = fwrite(buf, 1, r, tmp);
274 return got_error_from_errno("fwrite");
276 if (r != len && feof(orig)) {
279 return got_error(GOT_ERR_HUNK_FAILED);
285 static const struct got_error *
286 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
288 const struct got_error *err = NULL;
290 char mode = *h->lines[0];
294 long match_lineno = -1;
297 linelen = getline(&line, &linesize, orig);
300 err = got_error_from_errno("getline");
301 else if (match == -1)
302 err = got_error(GOT_ERR_HUNK_FAILED);
307 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
308 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
309 (mode == '+' && *lineno == h->old_from)) {
310 match = ftello(orig);
312 err = got_error_from_errno("ftello");
316 match_lineno = (*lineno)-1;
319 if (*lineno >= h->old_from && match != -1)
325 *lineno = match_lineno;
326 if (fseek(orig, match, SEEK_SET) == -1)
327 err = got_error_from_errno("fseek");
334 static const struct got_error *
335 test_hunk(FILE *orig, struct got_patch_hunk *h)
337 const struct got_error *err = NULL;
339 size_t linesize = 0, i = 0;
342 for (i = 0; i < h->len; ++i) {
343 switch (*h->lines[i]) {
348 linelen = getline(&line, &linesize, orig);
351 err = got_error_from_errno("getline");
354 GOT_ERR_HUNK_FAILED);
357 if (strcmp(h->lines[i] + 1, line)) {
358 err = got_error(GOT_ERR_HUNK_FAILED);
370 static const struct got_error *
371 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
375 for (i = 0; i < h->len; ++i) {
376 switch (*h->lines[i]) {
378 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
379 return got_error_from_errno("fprintf");
385 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
386 return got_error_from_errno("fprintf");
393 static const struct got_error *
394 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
397 const struct got_error *err = NULL;
398 struct got_patch_hunk *h;
408 if (p->old == NULL) { /* create */
409 h = STAILQ_FIRST(&p->head);
410 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
411 return got_error(GOT_ERR_PATCH_MALFORMED);
414 for (i = 0; i < h->len; ++i) {
415 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
416 return got_error_from_errno("fprintf");
421 if ((orig = fopen(path, "r")) == NULL) {
422 err = got_error_from_errno2("fopen", path);
426 if (fstat(fileno(orig), &sb) == -1) {
427 err = got_error_from_errno("fstat");
433 STAILQ_FOREACH(h, &p->head, entries) {
434 if (h->lines == NULL)
438 err = locate_hunk(orig, h, &pos, &lineno);
439 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
444 err = copy(tmp, orig, copypos, pos);
449 err = test_hunk(orig, h);
450 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
452 * try to apply the hunk again starting the search
453 * after the previous partial match.
455 if (fseek(orig, pos, SEEK_SET) == -1) {
456 err = got_error_from_errno("fseek");
459 linelen = getline(&line, &linesize, orig);
461 err = got_error_from_errno("getline");
470 if (lineno + 1 != h->old_from)
471 h->offset = lineno + 1 - h->old_from;
474 err = apply_hunk(tmp, h, &lineno);
478 copypos = ftello(orig);
480 err = got_error_from_errno("ftello");
485 if (p->new == NULL && sb.st_size != copypos) {
486 h = STAILQ_FIRST(&p->head);
487 h->err = got_error(GOT_ERR_HUNK_FAILED);
489 } else if (!nop && !feof(orig))
490 err = copy(tmp, orig, copypos, -1);
498 static const struct got_error *
499 build_pathlist(const char *p, char **path, struct got_pathlist_head *head,
500 struct got_worktree *worktree)
502 const struct got_error *err;
503 struct got_pathlist_entry *pe;
505 err = got_worktree_resolve_path(path, worktree, p);
507 err = got_pathlist_insert(&pe, head, *path, NULL);
511 static const struct got_error *
512 can_rm(void *arg, unsigned char status, unsigned char staged_status,
513 const char *path, struct got_object_id *blob_id,
514 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
515 int dirfd, const char *de_name)
517 if (status == GOT_STATUS_NONEXISTENT)
518 return got_error_set_errno(ENOENT, path);
519 if (status != GOT_STATUS_NO_CHANGE &&
520 status != GOT_STATUS_ADD &&
521 status != GOT_STATUS_MODIFY &&
522 status != GOT_STATUS_MODE_CHANGE)
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 can_add(void *arg, unsigned char status, unsigned char staged_status,
531 const char *path, struct got_object_id *blob_id,
532 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
533 int dirfd, const char *de_name)
535 if (status != GOT_STATUS_NONEXISTENT)
536 return got_error_path(path, GOT_ERR_FILE_STATUS);
540 static const struct got_error *
541 can_edit(void *arg, unsigned char status, unsigned char staged_status,
542 const char *path, struct got_object_id *blob_id,
543 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
544 int dirfd, const char *de_name)
546 if (status == GOT_STATUS_NONEXISTENT)
547 return got_error_set_errno(ENOENT, path);
548 if (status != GOT_STATUS_NO_CHANGE &&
549 status != GOT_STATUS_ADD &&
550 status != GOT_STATUS_MODIFY)
551 return got_error_path(path, GOT_ERR_FILE_STATUS);
552 if (staged_status == GOT_STATUS_DELETE)
553 return got_error_path(path, GOT_ERR_FILE_STATUS);
557 static const struct got_error *
558 check_file_status(struct got_patch *p, int file_renamed,
559 struct got_worktree *worktree, struct got_repository *repo,
560 struct got_pathlist_head *old, struct got_pathlist_head *new,
561 got_cancel_cb cancel_cb, void *cancel_arg)
563 static const struct got_error *err;
565 if (p->old != NULL && p->new == NULL)
566 return got_worktree_status(worktree, old, repo, 0,
567 can_rm, NULL, cancel_cb, cancel_arg);
568 else if (file_renamed) {
569 err = got_worktree_status(worktree, old, repo, 0,
570 can_rm, NULL, cancel_cb, cancel_arg);
573 return got_worktree_status(worktree, new, repo, 0,
574 can_add, NULL, cancel_cb, cancel_arg);
575 } else if (p->old == NULL)
576 return got_worktree_status(worktree, new, repo, 0,
577 can_add, NULL, cancel_cb, cancel_arg);
579 return got_worktree_status(worktree, new, repo, 0,
580 can_edit, NULL, cancel_cb, cancel_arg);
583 static const struct got_error *
584 report_progress(struct patch_args *pa, const char *old, const char *new,
585 unsigned char status, const struct got_error *orig_error)
587 const struct got_error *err;
588 struct got_patch_hunk *h;
590 err = pa->progress_cb(pa->progress_arg, old, new, status,
591 orig_error, 0, 0, 0, 0, 0, NULL);
595 STAILQ_FOREACH(h, pa->head, entries) {
596 if (h->offset == 0 && h->err == NULL)
599 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
600 h->old_from, h->old_lines, h->new_from, h->new_lines,
609 static const struct got_error *
610 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
613 return report_progress(arg, path, NULL, status, NULL);
616 static const struct got_error *
617 patch_add(void *arg, unsigned char status, const char *path)
619 return report_progress(arg, NULL, path, status, NULL);
622 static const struct got_error *
623 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
624 struct got_pathlist_head *oldpaths, struct got_pathlist_head *newpaths,
625 const char *oldpath, const char *newpath, struct got_patch *p,
626 int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
628 const struct got_error *err = NULL;
629 int file_renamed = 0;
630 char *tmppath = NULL, *template = NULL, *parent = NULL;;
632 mode_t mode = GOT_DEFAULT_FILE_MODE;
634 file_renamed = strcmp(oldpath, newpath);
636 err = check_file_status(p, file_renamed, worktree, repo, oldpaths,
637 newpaths, cancel_cb, cancel_arg);
641 if (p->old != NULL && p->new == NULL) {
643 * special case: delete a file. don't try to match
644 * the lines but just schedule the removal.
646 err = got_worktree_schedule_delete(worktree, &oldpaths,
647 0, NULL, delete_cb, delete_arg, repo, 0, 0);
651 if (asprintf(&template, "%s/got-patch",
652 got_worktree_get_root_path(worktree)) == -1) {
653 err = got_error_from_errno(template);
658 err = got_opentemp_named(&tmppath, &tmp, template);
661 err = patch_file(p, oldpath, tmp, nop, &mode);
668 if (p->old != NULL && p->new == NULL) {
669 err = got_worktree_schedule_delete(worktree, oldpaths,
670 0, NULL, patch_delete, pa, repo, 0, 0);
674 if (fchmod(fileno(tmp), mode) == -1) {
675 err = got_error_from_errno2("chmod", newpath);
679 if (rename(tmppath, newpath) == -1) {
680 if (errno != ENOENT) {
681 err = got_error_from_errno3("rename", tmppath,
686 err = got_path_dirname(&parent, newpath);
689 err = got_path_mkdir(parent);
692 if (rename(tmppath, newpath) == -1) {
693 err = got_error_from_errno3("rename", tmppath,
700 err = got_worktree_schedule_delete(worktree, oldpaths,
701 0, NULL, patch_delete, pa, repo, 0, 0);
703 err = got_worktree_schedule_add(worktree, newpaths,
704 patch_add, pa, repo, 1);
705 } else if (p->old == NULL)
706 err = got_worktree_schedule_add(worktree, newpaths,
707 patch_add, pa, repo, 1);
709 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
713 if (err != NULL && newpath != NULL && (file_renamed || p->old == NULL))
723 static const struct got_error *
724 resolve_paths(struct got_patch *p, struct got_worktree *worktree,
725 struct got_repository *repo, struct got_pathlist_head *oldpaths,
726 struct got_pathlist_head *newpaths, char **old, char **new)
728 const struct got_error *err;
730 TAILQ_INIT(oldpaths);
731 TAILQ_INIT(newpaths);
735 err = build_pathlist(p->old != NULL ? p->old : p->new, old,
740 err = build_pathlist(p->new != NULL ? p->new : p->old, new,
749 got_pathlist_free(oldpaths);
750 got_pathlist_free(newpaths);
754 const struct got_error *
755 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
756 int nop, got_patch_progress_cb progress_cb, void *progress_arg,
757 got_cancel_cb cancel_cb, void *cancel_arg)
759 const struct got_error *err = NULL;
760 struct got_pathlist_head oldpaths, newpaths;
761 char *oldpath, *newpath;
762 struct imsgbuf *ibuf;
763 int imsg_fds[2] = {-1, -1};
764 int done = 0, failed = 0;
767 ibuf = calloc(1, sizeof(*ibuf));
769 err = got_error_from_errno("calloc");
773 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
774 err = got_error_from_errno("socketpair");
780 err = got_error_from_errno("fork");
782 } else if (pid == 0) {
783 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
788 if (close(imsg_fds[1]) == -1) {
789 err = got_error_from_errno("close");
793 imsg_init(ibuf, imsg_fds[0]);
795 err = send_patch(ibuf, fd);
800 while (!done && err == NULL) {
802 struct patch_args pa;
804 pa.progress_cb = progress_cb;
805 pa.progress_arg = progress_arg;
808 err = recv_patch(ibuf, &done, &p);
812 err = resolve_paths(&p, worktree, repo, &oldpaths,
813 &newpaths, &oldpath, &newpath);
817 err = apply_patch(worktree, repo, &oldpaths, &newpaths,
818 oldpath, newpath, &p, nop, &pa, cancel_cb, cancel_arg);
821 /* recoverable errors */
822 if (err->code == GOT_ERR_FILE_STATUS ||
823 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
824 err = report_progress(&pa, p.old, p.new,
825 GOT_STATUS_CANNOT_UPDATE, err);
826 else if (err->code == GOT_ERR_HUNK_FAILED)
827 err = report_progress(&pa, p.old, p.new,
828 GOT_STATUS_CANNOT_UPDATE, NULL);
833 got_pathlist_free(&oldpaths);
834 got_pathlist_free(&newpaths);
842 if (fd != -1 && close(fd) == -1 && err == NULL)
843 err = got_error_from_errno("close");
846 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
847 err = got_error_from_errno("close");
848 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
849 err = got_error_from_errno("close");
850 if (err == NULL && failed)
851 err = got_error(GOT_ERR_PATCH_FAILED);