9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] [PATCH] patch: fix consecutive deletions
@ 2022-10-03 19:32 Michael Forney
  2022-10-07 18:09 ` ori
  0 siblings, 1 reply; 2+ messages in thread
From: Michael Forney @ 2022-10-03 19:32 UTC (permalink / raw)
  To: 9front


Consecutive delete hunks will both have newpath of /dev/null, but we
need to slurp oldpath between these hunks.
---
Proposed fix for the issue I reported on the mailing list a few days
ago. This retains the current patch file selection behavior, though I
still think we should adopt the standard behavior for compatibility
with external patches and tools.

diff 8f1867a19f7ca881a32878dcee08ddce11b5db36 185bd356498369a987e364293f3942e047b99f48
--- 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;

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [9front] [PATCH] patch: fix consecutive deletions
  2022-10-03 19:32 [9front] [PATCH] patch: fix consecutive deletions Michael Forney
@ 2022-10-07 18:09 ` ori
  0 siblings, 0 replies; 2+ messages in thread
From: ori @ 2022-10-07 18:09 UTC (permalink / raw)
  To: 9front

Quoth Michael Forney <mforney@mforney.org>:
> 
> Consecutive delete hunks will both have newpath of /dev/null, but we
> need to slurp oldpath between these hunks.
> ---
> Proposed fix for the issue I reported on the mailing list a few days
> ago. This retains the current patch file selection behavior, though I
> still think we should adopt the standard behavior for compatibility
> with external patches and tools.

Looks good to me.

Changing our current behavior should be painless -- nothing currently
should depend on it; git/patch is the heaviest user, and it hasn't been
around long enough for a long tail of external scripts to care.

> diff 8f1867a19f7ca881a32878dcee08ddce11b5db36 185bd356498369a987e364293f3942e047b99f48
> --- 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;


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-10-07 18:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-03 19:32 [9front] [PATCH] patch: fix consecutive deletions Michael Forney
2022-10-07 18:09 ` ori

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).