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
This commit is contained in:
Ori Bernstein 2022-07-02 20:13:31 +00:00
parent 6dbfe8c356
commit d457233c70

View file

@ -287,13 +287,13 @@ hunk:
while(1){ while(1){
if((ln = readline(f, &lnum)) == nil){ if((ln = readline(f, &lnum)) == nil){
if(oldcnt != h.oldcnt || newcnt != h.newcnt) 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); addhunk(p, &h);
break; break;
} }
switch(ln[0]){ switch(ln[0]){
default: default:
sysfatal("%s:%d: malformed hunk2", name, lnum); sysfatal("%s:%d: malformed hunk: leading junk", name, lnum);
goto out; goto out;
case '-': case '-':
addold(&h, ln); addold(&h, ln);
@ -303,6 +303,12 @@ hunk:
addnew(&h, ln); addnew(&h, ln);
newcnt++; newcnt++;
break; break;
case '\n':
addold(&h, " \n");
addnew(&h, " \n");
oldcnt++;
newcnt++;
break;
case ' ': case ' ':
addold(&h, ln); addold(&h, ln);
addnew(&h, ln); addnew(&h, ln);
@ -312,7 +318,7 @@ hunk:
} }
free(ln); free(ln);
if(oldcnt > h.oldcnt || newcnt > h.newcnt) 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) if(oldcnt < h.oldcnt || newcnt < h.newcnt)
continue; continue;