got-read-patch: preserve all \ lines as a cheap optimization got-read-patch was sending only the "\ No newline at end of file" lines that follows an addition (a "+" line). To be able to reverse patches in the future got_patch needs to know about all of these lines instead. No functional changes intended. ok stsp@
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
diff --git a/lib/patch.c b/lib/patch.c
index adf9459..f34e561 100644
--- a/lib/patch.c
+++ b/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_patch *p, int strip)
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_patch *p, int strip)
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_patch *p, int strip)
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)
(*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");
diff --git a/libexec/got-read-patch/got-read-patch.c b/libexec/got-read-patch/got-read-patch.c
index b72f7f8..879b77f 100644
--- a/libexec/got-read-patch/got-read-patch.c
+++ b/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;
}