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;
59 const struct got_error *err;
70 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
74 struct got_patch_hunk_head head;
78 got_patch_progress_cb progress_cb;
80 struct got_patch_hunk_head *head;
83 static const struct got_error *
84 send_patch(struct imsgbuf *ibuf, int fd)
86 const struct got_error *err = NULL;
88 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
90 err = got_error_from_errno(
91 "imsg_compose GOT_IMSG_PATCH_FILE");
96 if (imsg_flush(ibuf) == -1) {
97 err = got_error_from_errno("imsg_flush");
105 patch_free(struct got_patch *p)
107 struct got_patch_hunk *h;
110 while (!STAILQ_EMPTY(&p->head)) {
111 h = STAILQ_FIRST(&p->head);
112 STAILQ_REMOVE_HEAD(&p->head, entries);
114 for (i = 0; i < h->len; ++i)
124 static const struct got_error *
125 pushline(struct got_patch_hunk *h, const char *line)
130 if (h->len == h->cap) {
131 if ((newcap = h->cap * 1.5) == 0)
133 t = recallocarray(h->lines, h->cap, newcap,
134 sizeof(h->lines[0]));
136 return got_error_from_errno("recallocarray");
141 if ((t = strdup(line)) == NULL)
142 return got_error_from_errno("strdup");
144 h->lines[h->len++] = t;
148 static const struct got_error *
149 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
151 const struct got_error *err = NULL;
153 struct got_imsg_patch_hunk hdr;
154 struct got_imsg_patch patch;
155 struct got_patch_hunk *h = NULL;
158 memset(p, 0, sizeof(*p));
159 STAILQ_INIT(&p->head);
161 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
164 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
168 if (imsg.hdr.type != GOT_IMSG_PATCH) {
169 err = got_error(GOT_ERR_PRIVSEP_MSG);
172 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
173 if (datalen != sizeof(patch)) {
174 err = got_error(GOT_ERR_PRIVSEP_LEN);
177 memcpy(&patch, imsg.data, sizeof(patch));
178 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
179 err = got_error_from_errno("strdup");
182 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
183 err = got_error_from_errno("strdup");
186 if (p->old == NULL && p->new == NULL) {
187 err = got_error(GOT_ERR_PATCH_MALFORMED);
196 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
200 switch (imsg.hdr.type) {
201 case GOT_IMSG_PATCH_DONE:
203 case GOT_IMSG_PATCH_HUNK:
204 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
205 if (datalen != sizeof(hdr)) {
206 err = got_error(GOT_ERR_PRIVSEP_LEN);
209 memcpy(&hdr, imsg.data, sizeof(hdr));
210 if ((h = calloc(1, sizeof(*h))) == NULL) {
211 err = got_error_from_errno("calloc");
214 h->old_from = hdr.oldfrom;
215 h->old_lines = hdr.oldlines;
216 h->new_from = hdr.newfrom;
217 h->new_lines = hdr.newlines;
218 STAILQ_INSERT_TAIL(&p->head, h, entries);
220 case GOT_IMSG_PATCH_LINE:
222 err = got_error(GOT_ERR_PRIVSEP_MSG);
225 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
227 /* at least one char plus newline */
228 if (datalen < 2 || t[datalen-1] != '\0') {
229 err = got_error(GOT_ERR_PRIVSEP_MSG);
232 if (*t != ' ' && *t != '-' && *t != '+') {
233 err = got_error(GOT_ERR_PRIVSEP_MSG);
236 err = pushline(h, t);
241 err = got_error(GOT_ERR_PRIVSEP_MSG);
254 * Copy data from orig starting at copypos until pos into tmp.
255 * If pos is -1, copy until EOF.
257 static const struct got_error *
258 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
263 if (fseek(orig, copypos, SEEK_SET) == -1)
264 return got_error_from_errno("fseek");
266 while (pos == -1 || copypos < pos) {
269 len = MIN(len, (size_t)pos - copypos);
270 r = fread(buf, 1, len, orig);
271 if (r != len && ferror(orig))
272 return got_error_from_errno("fread");
273 w = fwrite(buf, 1, r, tmp);
275 return got_error_from_errno("fwrite");
277 if (r != len && feof(orig)) {
280 return got_error(GOT_ERR_HUNK_FAILED);
286 static const struct got_error *
287 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
289 const struct got_error *err = NULL;
291 char mode = *h->lines[0];
295 long match_lineno = -1;
298 linelen = getline(&line, &linesize, orig);
301 err = got_error_from_errno("getline");
302 else if (match == -1)
303 err = got_error(GOT_ERR_HUNK_FAILED);
308 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
309 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
310 (mode == '+' && *lineno == h->old_from)) {
311 match = ftello(orig);
313 err = got_error_from_errno("ftello");
317 match_lineno = (*lineno)-1;
320 if (*lineno >= h->old_from && match != -1)
326 *lineno = match_lineno;
327 if (fseek(orig, match, SEEK_SET) == -1)
328 err = got_error_from_errno("fseek");
335 static const struct got_error *
336 test_hunk(FILE *orig, struct got_patch_hunk *h)
338 const struct got_error *err = NULL;
340 size_t linesize = 0, i = 0;
343 for (i = 0; i < h->len; ++i) {
344 switch (*h->lines[i]) {
349 linelen = getline(&line, &linesize, orig);
352 err = got_error_from_errno("getline");
355 GOT_ERR_HUNK_FAILED);
358 if (strcmp(h->lines[i] + 1, line)) {
359 err = got_error(GOT_ERR_HUNK_FAILED);
371 static const struct got_error *
372 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
376 for (i = 0; i < h->len; ++i) {
377 switch (*h->lines[i]) {
379 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
380 return got_error_from_errno("fprintf");
386 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
387 return got_error_from_errno("fprintf");
394 static const struct got_error *
395 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
398 const struct got_error *err = NULL;
399 struct got_patch_hunk *h;
409 if (p->old == NULL) { /* create */
410 h = STAILQ_FIRST(&p->head);
411 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
412 return got_error(GOT_ERR_PATCH_MALFORMED);
415 for (i = 0; i < h->len; ++i) {
416 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
417 return got_error_from_errno("fprintf");
422 if ((orig = fopen(path, "r")) == NULL) {
423 err = got_error_from_errno2("fopen", path);
427 if (fstat(fileno(orig), &sb) == -1) {
428 err = got_error_from_errno("fstat");
434 STAILQ_FOREACH(h, &p->head, entries) {
435 if (h->lines == NULL)
439 err = locate_hunk(orig, h, &pos, &lineno);
440 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
445 err = copy(tmp, orig, copypos, pos);
450 err = test_hunk(orig, h);
451 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
453 * try to apply the hunk again starting the search
454 * after the previous partial match.
456 if (fseek(orig, pos, SEEK_SET) == -1) {
457 err = got_error_from_errno("fseek");
460 linelen = getline(&line, &linesize, orig);
462 err = got_error_from_errno("getline");
471 if (lineno + 1 != h->old_from)
472 h->offset = lineno + 1 - h->old_from;
475 err = apply_hunk(tmp, h, &lineno);
479 copypos = ftello(orig);
481 err = got_error_from_errno("ftello");
486 if (p->new == NULL && sb.st_size != copypos) {
487 h = STAILQ_FIRST(&p->head);
488 h->err = got_error(GOT_ERR_HUNK_FAILED);
490 } else if (!nop && !feof(orig))
491 err = copy(tmp, orig, copypos, -1);
499 static const struct got_error *
500 build_pathlist(const char *p, char **path, struct got_pathlist_head *head,
501 struct got_worktree *worktree)
503 const struct got_error *err;
504 struct got_pathlist_entry *pe;
506 err = got_worktree_resolve_path(path, worktree, p);
508 err = got_pathlist_insert(&pe, head, *path, NULL);
512 static const struct got_error *
513 can_rm(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 status != GOT_STATUS_MODE_CHANGE)
524 return got_error_path(path, GOT_ERR_FILE_STATUS);
525 if (staged_status == GOT_STATUS_DELETE)
526 return got_error_path(path, GOT_ERR_FILE_STATUS);
530 static const struct got_error *
531 can_add(void *arg, unsigned char status, unsigned char staged_status,
532 const char *path, struct got_object_id *blob_id,
533 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
534 int dirfd, const char *de_name)
536 if (status != GOT_STATUS_NONEXISTENT)
537 return got_error_path(path, GOT_ERR_FILE_STATUS);
541 static const struct got_error *
542 can_edit(void *arg, unsigned char status, unsigned char staged_status,
543 const char *path, struct got_object_id *blob_id,
544 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
545 int dirfd, const char *de_name)
547 if (status == GOT_STATUS_NONEXISTENT)
548 return got_error_set_errno(ENOENT, path);
549 if (status != GOT_STATUS_NO_CHANGE &&
550 status != GOT_STATUS_ADD &&
551 status != GOT_STATUS_MODIFY)
552 return got_error_path(path, GOT_ERR_FILE_STATUS);
553 if (staged_status == GOT_STATUS_DELETE)
554 return got_error_path(path, GOT_ERR_FILE_STATUS);
558 static const struct got_error *
559 check_file_status(struct got_patch *p, int file_renamed,
560 struct got_worktree *worktree, struct got_repository *repo,
561 struct got_pathlist_head *old, struct got_pathlist_head *new,
562 got_cancel_cb cancel_cb, void *cancel_arg)
564 static const struct got_error *err;
566 if (p->old != NULL && p->new == NULL)
567 return got_worktree_status(worktree, old, repo, 0,
568 can_rm, NULL, cancel_cb, cancel_arg);
569 else if (file_renamed) {
570 err = got_worktree_status(worktree, old, repo, 0,
571 can_rm, NULL, cancel_cb, cancel_arg);
574 return got_worktree_status(worktree, new, repo, 0,
575 can_add, NULL, cancel_cb, cancel_arg);
576 } else if (p->old == NULL)
577 return got_worktree_status(worktree, new, repo, 0,
578 can_add, NULL, cancel_cb, cancel_arg);
580 return got_worktree_status(worktree, new, repo, 0,
581 can_edit, NULL, cancel_cb, cancel_arg);
584 static const struct got_error *
585 report_progress(struct patch_args *pa, const char *old, const char *new,
586 unsigned char status, const struct got_error *orig_error)
588 const struct got_error *err;
589 struct got_patch_hunk *h;
591 err = pa->progress_cb(pa->progress_arg, old, new, status,
592 orig_error, 0, 0, 0, 0, 0, NULL);
596 STAILQ_FOREACH(h, pa->head, entries) {
597 if (h->offset == 0 && h->err == NULL)
600 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
601 h->old_from, h->old_lines, h->new_from, h->new_lines,
610 static const struct got_error *
611 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
614 return report_progress(arg, path, NULL, status, NULL);
617 static const struct got_error *
618 patch_add(void *arg, unsigned char status, const char *path)
620 return report_progress(arg, NULL, path, status, NULL);
623 static const struct got_error *
624 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
625 struct got_pathlist_head *oldpaths, struct got_pathlist_head *newpaths,
626 const char *oldpath, const char *newpath, struct got_patch *p,
627 int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
629 const struct got_error *err = NULL;
630 int file_renamed = 0;
631 char *tmppath = NULL, *template = NULL, *parent = NULL;;
633 mode_t mode = GOT_DEFAULT_FILE_MODE;
635 file_renamed = strcmp(oldpath, newpath);
637 err = check_file_status(p, file_renamed, worktree, repo, oldpaths,
638 newpaths, cancel_cb, cancel_arg);
642 if (asprintf(&template, "%s/got-patch",
643 got_worktree_get_root_path(worktree)) == -1) {
644 err = got_error_from_errno(template);
649 err = got_opentemp_named(&tmppath, &tmp, template);
652 err = patch_file(p, oldpath, tmp, nop, &mode);
659 if (p->old != NULL && p->new == NULL) {
660 err = got_worktree_schedule_delete(worktree, oldpaths,
661 0, NULL, patch_delete, pa, repo, 0, 0);
665 if (fchmod(fileno(tmp), mode) == -1) {
666 err = got_error_from_errno2("chmod", newpath);
670 if (rename(tmppath, newpath) == -1) {
671 if (errno != ENOENT) {
672 err = got_error_from_errno3("rename", tmppath,
677 err = got_path_dirname(&parent, newpath);
680 err = got_path_mkdir(parent);
683 if (rename(tmppath, newpath) == -1) {
684 err = got_error_from_errno3("rename", tmppath,
691 err = got_worktree_schedule_delete(worktree, oldpaths,
692 0, NULL, patch_delete, pa, repo, 0, 0);
694 err = got_worktree_schedule_add(worktree, newpaths,
695 patch_add, pa, repo, 1);
696 } else if (p->old == NULL)
697 err = got_worktree_schedule_add(worktree, newpaths,
698 patch_add, pa, repo, 1);
700 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
704 if (err != NULL && newpath != NULL && (file_renamed || p->old == NULL))
714 static const struct got_error *
715 resolve_paths(struct got_patch *p, struct got_worktree *worktree,
716 struct got_repository *repo, struct got_pathlist_head *oldpaths,
717 struct got_pathlist_head *newpaths, char **old, char **new)
719 const struct got_error *err;
721 TAILQ_INIT(oldpaths);
722 TAILQ_INIT(newpaths);
726 err = build_pathlist(p->old != NULL ? p->old : p->new, old,
731 err = build_pathlist(p->new != NULL ? p->new : p->old, new,
740 got_pathlist_free(oldpaths);
741 got_pathlist_free(newpaths);
745 const struct got_error *
746 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
747 int nop, got_patch_progress_cb progress_cb, void *progress_arg,
748 got_cancel_cb cancel_cb, void *cancel_arg)
750 const struct got_error *err = NULL;
751 struct got_pathlist_head oldpaths, newpaths;
752 char *oldpath, *newpath;
753 struct imsgbuf *ibuf;
754 int imsg_fds[2] = {-1, -1};
755 int done = 0, failed = 0;
758 ibuf = calloc(1, sizeof(*ibuf));
760 err = got_error_from_errno("calloc");
764 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
765 err = got_error_from_errno("socketpair");
771 err = got_error_from_errno("fork");
773 } else if (pid == 0) {
774 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
779 if (close(imsg_fds[1]) == -1) {
780 err = got_error_from_errno("close");
784 imsg_init(ibuf, imsg_fds[0]);
786 err = send_patch(ibuf, fd);
791 while (!done && err == NULL) {
793 struct patch_args pa;
795 pa.progress_cb = progress_cb;
796 pa.progress_arg = progress_arg;
799 err = recv_patch(ibuf, &done, &p);
803 err = resolve_paths(&p, worktree, repo, &oldpaths,
804 &newpaths, &oldpath, &newpath);
808 err = apply_patch(worktree, repo, &oldpaths, &newpaths,
809 oldpath, newpath, &p, nop, &pa, cancel_cb, cancel_arg);
812 /* recoverable errors */
813 if (err->code == GOT_ERR_FILE_STATUS ||
814 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
815 err = report_progress(&pa, p.old, p.new,
816 GOT_STATUS_CANNOT_UPDATE, err);
817 else if (err->code == GOT_ERR_HUNK_FAILED)
818 err = report_progress(&pa, p.old, p.new,
819 GOT_STATUS_CANNOT_UPDATE, NULL);
824 got_pathlist_free(&oldpaths);
825 got_pathlist_free(&newpaths);
833 if (fd != -1 && close(fd) == -1 && err == NULL)
834 err = got_error_from_errno("close");
837 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
838 err = got_error_from_errno("close");
839 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
840 err = got_error_from_errno("close");
841 if (err == NULL && failed)
842 err = got_error(GOT_ERR_PATCH_FAILED);