# HG changeset patch # User Patrick Mezard # Date 2008-01-12 18:35:11 # Node ID 03ce5a919ae36ac5622562f5b3d06945c99d0340 # Parent 03f550f9b554fbb1c9d60d720537549cc0613359 patch: handle empty vs no file in git patches (issue906) diff --git a/mercurial/patch.py b/mercurial/patch.py --- a/mercurial/patch.py +++ b/mercurial/patch.py @@ -499,7 +499,7 @@ class patchfile: return -1 class hunk: - def __init__(self, desc, num, lr, context): + def __init__(self, desc, num, lr, context, gitpatch=None): self.number = num self.desc = desc self.hunk = [ desc ] @@ -509,6 +509,7 @@ class hunk: self.read_context_hunk(lr) else: self.read_unified_hunk(lr) + self.gitpatch = gitpatch def read_unified_hunk(self, lr): m = unidesc.match(self.desc) @@ -663,10 +664,12 @@ class hunk: return len(self.a) == self.lena and len(self.b) == self.lenb def createfile(self): - return self.starta == 0 and self.lena == 0 + create = self.gitpatch is None or self.gitpatch.op == 'ADD' + return self.starta == 0 and self.lena == 0 and create def rmfile(self): - return self.startb == 0 and self.lenb == 0 + remove = self.gitpatch is None or self.gitpatch.op == 'DELETE' + return self.startb == 0 and self.lenb == 0 and remove def fuzzit(self, l, fuzz, toponly): # this removes context lines from the top and bottom of list 'l'. It @@ -918,7 +921,8 @@ def applydiff(ui, fp, changed, strip=1, try: if context == None and x.startswith('***************'): context = True - current_hunk = hunk(x, hunknum + 1, lr, context) + gpatch = changed.get(bfile[2:], (None, None))[1] + current_hunk = hunk(x, hunknum + 1, lr, context, gpatch) except PatchError, err: ui.debug(err) current_hunk = None diff --git a/tests/test-import b/tests/test-import --- a/tests/test-import +++ b/tests/test-import @@ -153,3 +153,31 @@ echo line0 >> a hg ci -m brancha hg import -v tip.patch cd .. + +# Test hunk touching empty files (issue906) +hg init empty +cd empty +touch a +touch b1 +touch c1 +echo d > d +hg ci -Am init +echo a > a +echo b > b1 +hg mv b1 b2 +echo c > c1 +hg copy c1 c2 +rm d +touch d +hg diff --git +hg ci -m empty +hg export --git tip > empty.diff +hg up -C 0 +hg import empty.diff +for name in a b1 b2 c1 c2 d; +do + echo % $name file + test -f $name && cat $name +done +cd .. + diff --git a/tests/test-import.out b/tests/test-import.out --- a/tests/test-import.out +++ b/tests/test-import.out @@ -132,3 +132,48 @@ applying tip.patch patching file a Hunk #1 succeeded at 1 with fuzz 2 (offset -2 lines). a +adding a +adding b1 +adding c1 +adding d +diff --git a/a b/a +--- a/a ++++ b/a +@@ -0,0 +1,1 @@ ++a +diff --git a/b1 b/b2 +rename from b1 +rename to b2 +--- a/b1 ++++ b/b2 +@@ -0,0 +1,1 @@ ++b +diff --git a/c1 b/c1 +--- a/c1 ++++ b/c1 +@@ -0,0 +1,1 @@ ++c +diff --git a/c1 b/c2 +copy from c1 +copy to c2 +--- a/c1 ++++ b/c2 +@@ -0,0 +1,1 @@ ++c +diff --git a/d b/d +--- a/d ++++ b/d +@@ -1,1 +0,0 @@ +-d +4 files updated, 0 files merged, 2 files removed, 0 files unresolved +applying empty.diff +% a file +a +% b1 file +% b2 file +b +% c1 file +c +% c2 file +c +% d file