patch: handle empty vs no file in git patches (issue906)
authorPatrick Mezard <pmezard@gmail.com>
Sat, 12 Jan 2008 19:35:11 +0100
changeset 5852 03ce5a919ae3
parent 5851 03f550f9b554
child 5853 deb0d3518674
child 5879 cacfeee38870
patch: handle empty vs no file in git patches (issue906)
mercurial/patch.py
tests/test-import
tests/test-import.out
--- a/mercurial/patch.py	Sat Jan 12 20:43:09 2008 +0100
+++ b/mercurial/patch.py	Sat Jan 12 19:35:11 2008 +0100
@@ -499,7 +499,7 @@
         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 @@
             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 @@
         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 @@
             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
--- a/tests/test-import	Sat Jan 12 20:43:09 2008 +0100
+++ b/tests/test-import	Sat Jan 12 19:35:11 2008 +0100
@@ -153,3 +153,31 @@
 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 ..
+
--- a/tests/test-import.out	Sat Jan 12 20:43:09 2008 +0100
+++ b/tests/test-import.out	Sat Jan 12 19:35:11 2008 +0100
@@ -132,3 +132,48 @@
 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