Download raw body.
got_patch refactoring: preserve all \ lines
got-read-patch ignores "\ No newline at end of file" after a "-" lines: this is to simplify the matching routines and because we're really interested only in "no newline at eof" markers that follows a "+" line. This, however, gets in the way of reversing patches because then a "\" marker after a "-" suddenly becomes interesting... The diff also includes a small tweak in apply_hunk to aid readability since i'm already touching it: instead of checking wether we've reached the end of the lines array, just count how many '+' we've seen until now. Now that I think of it this could count as a bugfix because strange diffs with additions before deletions would probably fail to honour the "no newline at eof" marker without it. Oh well :) Regress is passing and i have a follow-up diff to add -R to got patch. ok? ----------------------------------------------- commit b0ba446a8a36b66c9e4d93d462be7953ebc717ed from: Omar Polo <op@omarpolo.com> date: Tue Apr 12 10:38:42 2022 UTC got-read-patch: preserve all the \ lines send all "\ No newline at end of file" lines, not only the ones after a "+" line. It will allow to reverse patches later. diff 103f19a1f3b497422681f16a67d841419eb9cdd8 0e6f5b49e77b526f4d1cfcada5b3e4a728dbe44f blob - 9b49b4b1349c2d2994dfa6e667c9265975aea642 blob + fd8a331bcf22f2eae5872f93fe383f4de9dd7af6 --- lib/patch.c +++ lib/patch.c @@ -55,7 +55,8 @@ struct got_patch_hunk { STAILQ_ENTRY(got_patch_hunk) entries; const struct got_error *err; long offset; - int nonl; + int old_nonl; + int new_nonl; long old_from; long old_lines; long new_from; @@ -152,6 +153,7 @@ recv_patch(struct imsgbuf *ibuf, int *done, struct got struct got_imsg_patch patch; struct got_patch_hunk *h = NULL; size_t datalen; + int lastmode = -1; memset(p, 0, sizeof(*p)); STAILQ_INIT(&p->head); @@ -215,10 +217,11 @@ recv_patch(struct imsgbuf *ibuf, int *done, struct got case GOT_IMSG_PATCH_DONE: goto done; case GOT_IMSG_PATCH_HUNK: - if (h != NULL && h->nonl) { + if (h != NULL && (h->old_nonl || h->new_nonl)) { err = got_error(GOT_ERR_PATCH_MALFORMED); goto done; } + lastmode = -1; datalen = imsg.hdr.len - IMSG_HEADER_SIZE; if (datalen != sizeof(hdr)) { err = got_error(GOT_ERR_PRIVSEP_LEN); @@ -252,14 +255,20 @@ recv_patch(struct imsgbuf *ibuf, int *done, struct got err = got_error(GOT_ERR_PRIVSEP_MSG); goto done; } - if (h->nonl) - err = got_error(GOT_ERR_PATCH_MALFORMED); - if (*t == '\\') - h->nonl = 1; - else + + if (*t != '\\') err = pushline(h, t); + else if (lastmode == '-') + h->old_nonl = 1; + else if (lastmode == '+') + h->new_nonl = 1; + else + err = got_error(GOT_ERR_PATCH_MALFORMED); + if (err) goto done; + + lastmode = *t; break; default: err = got_error(GOT_ERR_PRIVSEP_MSG); @@ -399,7 +408,7 @@ done: static const struct got_error * apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno) { - size_t i = 0; + size_t i, new = 0; for (i = 0; i < h->len; ++i) { switch (*h->lines[i]) { @@ -411,9 +420,10 @@ apply_hunk(FILE *tmp, struct got_patch_hunk *h, long * (*lineno)++; break; case '+': + new++; if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) return got_error_from_errno("fprintf"); - if (i != h->len - 1 || !h->nonl) { + if (new != h->new_lines || !h->new_nonl) { if (fprintf(tmp, "\n") < 0) return got_error_from_errno( "fprintf"); blob - b72f7f88829c3931ef203cba5d632edc4dc06195 blob + 879b77f8142d65729f38b9389c48cf5a78299027 --- libexec/got-read-patch/got-read-patch.c +++ libexec/got-read-patch/got-read-patch.c @@ -288,7 +288,7 @@ send_line(const char *line) } static const struct got_error * -peek_special_line(FILE *fp, int send) +peek_special_line(FILE *fp) { const struct got_error *err; int ch; @@ -299,7 +299,7 @@ peek_special_line(FILE *fp, int send) return NULL; } - if (ch == '\\' && send) { + if (ch == '\\') { err = send_line("\\"); if (err) return err; @@ -396,7 +396,7 @@ parse_hunk(FILE *fp, int *ok) if ((ch == '-' && leftold == 0) || (ch == '+' && leftnew == 0)) { - err = peek_special_line(fp, ch == '+'); + err = peek_special_line(fp); if (err) goto done; }
got_patch refactoring: preserve all \ lines