From ee46f70288c4a41ce2884a9ae676c61e131bdd35 Mon Sep 17 00:00:00 2001 From: Michael Forney Date: Mon, 3 Oct 2022 19:32:17 +0000 Subject: patch: fix consecutive deletions Consecutive delete hunks will both have newpath of /dev/null, but we need to reslurp oldpath between these hunks. --- sys/src/cmd/patch.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'sys/src/cmd/patch.c') diff --git a/sys/src/cmd/patch.c b/sys/src/cmd/patch.c index def9f05ca..5d882150b 100644 --- a/sys/src/cmd/patch.c +++ b/sys/src/cmd/patch.c @@ -556,23 +556,35 @@ append(char *o, int *sz, char *s, char *e) 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; 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 @@ apply(Patch *p, char *fname) 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; -- cgit v1.2.3