code: plan9front

Download patch

ref: ee46f70288c4a41ce2884a9ae676c61e131bdd35
parent: 01df41be0b8ef0ef307f517ea5d8048cd35a48f9
author: Michael Forney <mforney@mforney.org>
date: Mon Oct 3 15:32:17 EDT 2022

patch: fix consecutive deletions

Consecutive delete hunks will both have newpath of /dev/null, but we
need to reslurp oldpath between these hunks.

--- a/sys/src/cmd/patch.c
+++ b/sys/src/cmd/patch.c
@@ -556,9 +556,9 @@
 int
 apply(Patch *p, char *fname)
 {
-	char *o, *s, *e, *curfile;
+	char *o, *s, *e, *curfile, *nextfile;
 	int i, osz;
-	Hunk *h;
+	Hunk *h, *prevh;
 	Fbuf f;
 
 	e = nil;
@@ -565,14 +565,26 @@
 	o = nil;
 	osz = 0;
 	curfile = nil;
+	h = nil;
+	prevh = nil;
 	for(i = 0; i < p->nhunk; i++){
 		h = &p->hunk[i];
-		if(curfile == nil || strcmp(curfile, h->newpath) != 0){
+		if(strcmp(h->newpath, "/dev/null") == 0)
+			nextfile = h->oldpath;
+		else
+			nextfile = h->newpath;
+		if(curfile == nil || strcmp(curfile, nextfile) != 0){
+			if(curfile != nil){
+				if(!dryrun)
+					o = append(o, &osz, e, f.buf + f.len);
+				blat(prevh->oldpath, prevh->newpath, o, osz);
+				osz = 0;
+			}
 			if(!dryrun){
 				slurp(&f, h->oldpath);
 				e = f.buf;
 			}
-			curfile = h->newpath;
+			curfile = nextfile;
 		}
 		if(!dryrun){
 			s = e;
@@ -581,12 +593,12 @@
 			o = append(o, &osz, h->new, h->new + h->newlen);
 			e += h->oldlen;
 		}
-		if(i+1 == p->nhunk || strcmp(curfile, p->hunk[i+1].newpath) != 0){
-			if(!dryrun)
-				o = append(o, &osz, e, f.buf + f.len);
-			blat(h->oldpath, h->newpath, o, osz);
-			osz = 0;
-		}
+		prevh = h;
+	}
+	if(curfile != nil){
+		if(!dryrun)
+			o = append(o, &osz, e, f.buf + f.len);
+		blat(h->oldpath, h->newpath, o, osz);
 	}
 	free(o);
 	return 0;