code: plan9front

Download patch

ref: d457233c70ef6aa28861dd2978e92968ffba0920
parent: 6dbfe8c3562b8ccf630ed0e3099b47c8d2730d14
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Jul 2 16:13:31 EDT 2022

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

--- a/sys/src/cmd/patch.c
+++ b/sys/src/cmd/patch.c
@@ -287,13 +287,13 @@
 	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 @@
 			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 @@
 		}
 		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;