From d457233c70ef6aa28861dd2978e92968ffba0920 Mon Sep 17 00:00:00 2001 From: Ori Bernstein Date: Sat, 2 Jul 2022 20:13:31 +0000 Subject: [PATCH] patch: handle stripped/empty lines mid-hunk Some programs will strip trailing spaces from hunks; in this case we want to treat the line as though it came with a leading space. This fixes applying some patches, such as [PATCH] Permissions for child boards in /srv --- sys/src/cmd/patch.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/sys/src/cmd/patch.c b/sys/src/cmd/patch.c index 3da0674ff..d1b9db5fe 100644 --- a/sys/src/cmd/patch.c +++ b/sys/src/cmd/patch.c @@ -287,13 +287,13 @@ hunk: while(1){ if((ln = readline(f, &lnum)) == nil){ if(oldcnt != h.oldcnt || newcnt != h.newcnt) - sysfatal("%s:%d: malformed hunk", name, lnum); + sysfatal("%s:%d: malformed hunk: mismatched counts", name, lnum); addhunk(p, &h); break; } switch(ln[0]){ default: - sysfatal("%s:%d: malformed hunk2", name, lnum); + sysfatal("%s:%d: malformed hunk: leading junk", name, lnum); goto out; case '-': addold(&h, ln); @@ -303,6 +303,12 @@ hunk: addnew(&h, ln); newcnt++; break; + case '\n': + addold(&h, " \n"); + addnew(&h, " \n"); + oldcnt++; + newcnt++; + break; case ' ': addold(&h, ln); addnew(&h, ln); @@ -312,7 +318,7 @@ hunk: } free(ln); if(oldcnt > h.oldcnt || newcnt > h.newcnt) - sysfatal("%s:%d: malformed hunk", name, lnum); + sysfatal("%s:%d: malformed hunk: oversized hunk", name, lnum); if(oldcnt < h.oldcnt || newcnt < h.newcnt) continue;