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 we may want to support:
19 * + support indented patches?
20 * + support other kinds of patches?
23 #include <sys/types.h>
24 #include <sys/queue.h>
25 #include <sys/socket.h>
39 #include "got_error.h"
40 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_patch.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_privsep.h"
52 #define MIN(a, b) ((a) < (b) ? (a) : (b))
54 struct got_patch_hunk {
55 STAILQ_ENTRY(got_patch_hunk) entries;
56 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, int strip)
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;
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));
179 /* automatically set strip=1 for git-style diffs */
180 if (strip == -1 && patch.git &&
181 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
182 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
185 /* prefer the new name if not /dev/null for not git-style diffs */
186 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
187 err = got_path_strip(&p->old, patch.new, strip);
190 } else if (*patch.old != '\0') {
191 err = got_path_strip(&p->old, patch.old, strip);
196 if (*patch.new != '\0') {
197 err = got_path_strip(&p->new, patch.new, strip);
202 if (p->old == NULL && p->new == NULL) {
203 err = got_error(GOT_ERR_PATCH_MALFORMED);
212 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
216 switch (imsg.hdr.type) {
217 case GOT_IMSG_PATCH_DONE:
218 if (h != NULL && h->len == 0)
219 err = got_error(GOT_ERR_PATCH_MALFORMED);
221 case GOT_IMSG_PATCH_HUNK:
223 (h->len == 0 || h->old_nonl || h->new_nonl)) {
224 err = got_error(GOT_ERR_PATCH_MALFORMED);
228 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
229 if (datalen != sizeof(hdr)) {
230 err = got_error(GOT_ERR_PRIVSEP_LEN);
233 memcpy(&hdr, imsg.data, sizeof(hdr));
234 if ((h = calloc(1, sizeof(*h))) == NULL) {
235 err = got_error_from_errno("calloc");
238 h->old_from = hdr.oldfrom;
239 h->old_lines = hdr.oldlines;
240 h->new_from = hdr.newfrom;
241 h->new_lines = hdr.newlines;
242 STAILQ_INSERT_TAIL(&p->head, h, entries);
244 case GOT_IMSG_PATCH_LINE:
246 err = got_error(GOT_ERR_PRIVSEP_MSG);
249 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
251 /* at least one char */
252 if (datalen < 2 || t[datalen-1] != '\0') {
253 err = got_error(GOT_ERR_PRIVSEP_MSG);
256 if (*t != ' ' && *t != '-' && *t != '+' &&
258 err = got_error(GOT_ERR_PRIVSEP_MSG);
263 err = pushline(h, t);
264 else if (lastmode == '-')
266 else if (lastmode == '+')
269 err = got_error(GOT_ERR_PATCH_MALFORMED);
277 err = got_error(GOT_ERR_PRIVSEP_MSG);
290 * Copy data from orig starting at copypos until pos into tmp.
291 * If pos is -1, copy until EOF.
293 static const struct got_error *
294 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
299 if (fseeko(orig, copypos, SEEK_SET) == -1)
300 return got_error_from_errno("fseeko");
302 while (pos == -1 || copypos < pos) {
305 len = MIN(len, (size_t)pos - copypos);
306 r = fread(buf, 1, len, orig);
307 if (r != len && ferror(orig))
308 return got_error_from_errno("fread");
309 w = fwrite(buf, 1, r, tmp);
311 return got_error_from_errno("fwrite");
313 if (r != len && feof(orig)) {
316 return got_error(GOT_ERR_HUNK_FAILED);
322 static const struct got_error *
323 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
325 const struct got_error *err = NULL;
327 char mode = *h->lines[0];
331 long match_lineno = -1;
334 linelen = getline(&line, &linesize, orig);
337 err = got_error_from_errno("getline");
338 else if (match == -1)
339 err = got_error(GOT_ERR_HUNK_FAILED);
342 if (line[linelen - 1] == '\n')
343 line[linelen - 1] = '\0';
346 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
347 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
348 (mode == '+' && *lineno == h->old_from)) {
349 match = ftello(orig);
351 err = got_error_from_errno("ftello");
355 match_lineno = (*lineno)-1;
358 if (*lineno >= h->old_from && match != -1)
364 *lineno = match_lineno;
365 if (fseeko(orig, match, SEEK_SET) == -1)
366 err = got_error_from_errno("fseeko");
373 static const struct got_error *
374 test_hunk(FILE *orig, struct got_patch_hunk *h)
376 const struct got_error *err = NULL;
378 size_t linesize = 0, i = 0;
381 for (i = 0; i < h->len; ++i) {
382 switch (*h->lines[i]) {
387 linelen = getline(&line, &linesize, orig);
390 err = got_error_from_errno("getline");
393 GOT_ERR_HUNK_FAILED);
396 if (line[linelen - 1] == '\n')
397 line[linelen - 1] = '\0';
398 if (strcmp(h->lines[i] + 1, line)) {
399 err = got_error(GOT_ERR_HUNK_FAILED);
411 static const struct got_error *
412 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
416 for (i = 0; i < h->len; ++i) {
417 switch (*h->lines[i]) {
419 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
420 return got_error_from_errno("fprintf");
427 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
428 return got_error_from_errno("fprintf");
429 if (new != h->new_lines || !h->new_nonl) {
430 if (fprintf(tmp, "\n") < 0)
431 return got_error_from_errno(
440 static const struct got_error *
441 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
444 const struct got_error *err = NULL;
445 struct got_patch_hunk *h;
454 if (p->old == NULL) { /* create */
455 h = STAILQ_FIRST(&p->head);
456 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
457 return got_error(GOT_ERR_PATCH_MALFORMED);
460 return apply_hunk(tmp, h, &lineno);
463 if ((orig = fopen(path, "r")) == NULL) {
464 err = got_error_from_errno2("fopen", path);
468 if (fstat(fileno(orig), &sb) == -1) {
469 err = got_error_from_errno("fstat");
475 STAILQ_FOREACH(h, &p->head, entries) {
477 err = locate_hunk(orig, h, &pos, &lineno);
478 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
483 err = copy(tmp, orig, copypos, pos);
488 err = test_hunk(orig, h);
489 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
491 * try to apply the hunk again starting the search
492 * after the previous partial match.
494 if (fseeko(orig, pos, SEEK_SET) == -1) {
495 err = got_error_from_errno("fseeko");
498 linelen = getline(&line, &linesize, orig);
500 err = got_error_from_errno("getline");
509 if (lineno + 1 != h->old_from)
510 h->offset = lineno + 1 - h->old_from;
513 err = apply_hunk(tmp, h, &lineno);
517 copypos = ftello(orig);
519 err = got_error_from_errno("ftello");
524 if (p->new == NULL && sb.st_size != copypos) {
525 h = STAILQ_FIRST(&p->head);
526 h->err = got_error(GOT_ERR_HUNK_FAILED);
528 } else if (!nop && !feof(orig))
529 err = copy(tmp, orig, copypos, -1);
537 static const struct got_error *
538 report_progress(struct patch_args *pa, const char *old, const char *new,
539 unsigned char status, const struct got_error *orig_error)
541 const struct got_error *err;
542 struct got_patch_hunk *h;
544 err = pa->progress_cb(pa->progress_arg, old, new, status,
545 orig_error, 0, 0, 0, 0, 0, NULL);
549 STAILQ_FOREACH(h, pa->head, entries) {
550 if (h->offset == 0 && h->err == NULL)
553 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
554 h->old_from, h->old_lines, h->new_from, h->new_lines,
563 static const struct got_error *
564 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
567 return report_progress(arg, path, NULL, status, NULL);
570 static const struct got_error *
571 patch_add(void *arg, unsigned char status, const char *path)
573 return report_progress(arg, NULL, path, status, NULL);
576 static const struct got_error *
577 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
578 struct got_fileindex *fileindex, const char *old, const char *new,
579 struct got_patch *p, int nop, struct patch_args *pa,
580 got_cancel_cb cancel_cb, void *cancel_arg)
582 const struct got_error *err = NULL;
583 int file_renamed = 0;
584 char *oldpath = NULL, *newpath = NULL;
585 char *tmppath = NULL, *template = NULL, *parent = NULL;;
587 mode_t mode = GOT_DEFAULT_FILE_MODE;
589 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
591 err = got_error_from_errno("asprintf");
595 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
597 err = got_error_from_errno("asprintf");
601 file_renamed = strcmp(oldpath, newpath);
603 if (asprintf(&template, "%s/got-patch",
604 got_worktree_get_root_path(worktree)) == -1) {
605 err = got_error_from_errno(template);
610 err = got_opentemp_named(&tmppath, &tmp, template);
613 err = patch_file(p, oldpath, tmp, nop, &mode);
620 if (p->old != NULL && p->new == NULL) {
621 err = got_worktree_patch_schedule_rm(old, repo, worktree,
622 fileindex, patch_delete, pa);
626 if (fchmod(fileno(tmp), mode) == -1) {
627 err = got_error_from_errno2("chmod", tmppath);
631 if (rename(tmppath, newpath) == -1) {
632 if (errno != ENOENT) {
633 err = got_error_from_errno3("rename", tmppath,
638 err = got_path_dirname(&parent, newpath);
641 err = got_path_mkdir(parent);
644 if (rename(tmppath, newpath) == -1) {
645 err = got_error_from_errno3("rename", tmppath,
652 err = got_worktree_patch_schedule_rm(old, repo, worktree,
653 fileindex, patch_delete, pa);
655 err = got_worktree_patch_schedule_add(new, repo,
656 worktree, fileindex, patch_add,
660 } else if (p->old == NULL) {
661 err = got_worktree_patch_schedule_add(new, repo, worktree,
662 fileindex, patch_add, pa);
666 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
680 reverse_patch(struct got_patch *p)
682 struct got_patch_hunk *h;
686 STAILQ_FOREACH(h, &p->head, entries) {
688 h->old_from = h->new_from;
692 h->old_lines = h->new_lines;
696 h->old_nonl = h->new_nonl;
699 for (i = 0; i < h->len; ++i) {
700 if (*h->lines[i] == '+')
702 else if (*h->lines[i] == '-')
708 const struct got_error *
709 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
710 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
711 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
713 const struct got_error *err = NULL, *complete_err = NULL;
714 struct got_fileindex *fileindex = NULL;
715 char *fileindex_path = NULL;
716 char *oldpath, *newpath;
717 struct imsgbuf *ibuf;
718 int imsg_fds[2] = {-1, -1};
719 int done = 0, failed = 0;
722 ibuf = calloc(1, sizeof(*ibuf));
724 err = got_error_from_errno("calloc");
728 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
729 err = got_error_from_errno("socketpair");
735 err = got_error_from_errno("fork");
737 } else if (pid == 0) {
738 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
743 if (close(imsg_fds[1]) == -1) {
744 err = got_error_from_errno("close");
748 imsg_init(ibuf, imsg_fds[0]);
750 err = send_patch(ibuf, fd);
755 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
760 while (!done && err == NULL) {
762 struct patch_args pa;
764 pa.progress_cb = progress_cb;
765 pa.progress_arg = progress_arg;
768 err = recv_patch(ibuf, &done, &p, strip);
775 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
776 &newpath, worktree, repo, fileindex);
778 err = apply_patch(worktree, repo, fileindex, oldpath,
779 newpath, &p, nop, &pa, cancel_cb, cancel_arg);
782 /* recoverable errors */
783 if (err->code == GOT_ERR_FILE_STATUS ||
784 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
785 err = report_progress(&pa, p.old, p.new,
786 GOT_STATUS_CANNOT_UPDATE, err);
787 else if (err->code == GOT_ERR_HUNK_FAILED)
788 err = report_progress(&pa, p.old, p.new,
789 GOT_STATUS_CANNOT_UPDATE, NULL);
801 if (fileindex != NULL)
802 complete_err = got_worktree_patch_complete(fileindex,
804 if (complete_err && err == NULL)
806 free(fileindex_path);
807 if (fd != -1 && close(fd) == -1 && err == NULL)
808 err = got_error_from_errno("close");
811 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
812 err = got_error_from_errno("close");
813 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
814 err = got_error_from_errno("close");
815 if (err == NULL && failed)
816 err = got_error(GOT_ERR_PATCH_FAILED);