changeset 3716:ab5600428b08

handle files with both git binary patches and copy/rename ops
author Alexis S. L. Carvalho <alexis@cecm.usp.br>
date Mon, 27 Nov 2006 22:03:31 -0200
parents 6cb3aca69cdc
children 9e248cfd8b94
files mercurial/patch.py tests/test-git-import tests/test-git-import.out
diffstat 3 files changed, 36 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/patch.py	Mon Nov 27 15:27:09 2006 -0800
+++ b/mercurial/patch.py	Mon Nov 27 22:03:31 2006 -0200
@@ -114,6 +114,10 @@
         return None, message, user, date
     return tmpname, message, user, date
 
+GP_PATCH  = 1 << 0  # we have to run patch
+GP_FILTER = 1 << 1  # there's some copy/rename operation
+GP_BINARY = 1 << 2  # there's a binary patch
+
 def readgitpatch(patchname):
     """extract git-style metadata about patches from <patchname>"""
     class gitpatch:
@@ -133,7 +137,7 @@
     gp = None
     gitpatches = []
     # Can have a git patch with only metadata, causing patch to complain
-    dopatch = False
+    dopatch = 0
 
     lineno = 0
     for line in pf:
@@ -150,11 +154,10 @@
             if line.startswith('--- '):
                 if gp.op in ('COPY', 'RENAME'):
                     gp.copymod = True
-                    dopatch = 'filter'
+                    dopatch |= GP_FILTER
                 gitpatches.append(gp)
                 gp = None
-                if not dopatch:
-                    dopatch = True
+                dopatch |= GP_PATCH
                 continue
             if line.startswith('rename from '):
                 gp.op = 'RENAME'
@@ -174,14 +177,13 @@
             elif line.startswith('new mode '):
                 gp.mode = int(line.rstrip()[-3:], 8)
             elif line.startswith('GIT binary patch'):
-                if not dopatch:
-                    dopatch = 'binary'
+                dopatch |= GP_BINARY
                 gp.binary = True
     if gp:
         gitpatches.append(gp)
 
     if not gitpatches:
-        dopatch = True
+        dopatch = GP_PATCH
 
     return (dopatch, gitpatches)
 
@@ -312,13 +314,14 @@
 
     fuzz = False
     if dopatch:
-        if dopatch in ('filter', 'binary'):
+        filterpatch = dopatch & (GP_FILTER | GP_BINARY)
+        if filterpatch:
             patchname = dogitpatch(patchname, gitpatches, cwd=cwd)
         try:
-            if dopatch != 'binary':
+            if dopatch & GP_PATCH:
                 fuzz = __patch(patchname)
         finally:
-            if dopatch == 'filter':
+            if filterpatch:
                 os.unlink(patchname)
 
     return fuzz
--- a/tests/test-git-import	Mon Nov 27 15:27:09 2006 -0800
+++ b/tests/test-git-import	Mon Nov 27 22:03:31 2006 -0200
@@ -143,3 +143,22 @@
 hg cat rename3
 echo
 hg cat rename3-2
+
+echo foo > foo
+hg add foo
+hg ci -m 'add foo'
+echo % binary files and regular patch hunks
+hg import -m binaryregular - <<EOF
+diff --git a/binary b/binary
+new file mode 100644
+index 0000000000000000000000000000000000000000..593f4708db84ac8fd0f5cc47c634f38c013fe9e4
+GIT binary patch
+literal 4
+Lc\${NkU|;|M00aO5
+
+diff --git a/foo b/foo2
+rename from foo
+rename to foo2
+EOF
+cat foo2
+hg manifest | grep binary
--- a/tests/test-git-import.out	Mon Nov 27 15:27:09 2006 -0800
+++ b/tests/test-git-import.out	Mon Nov 27 22:03:31 2006 -0200
@@ -54,3 +54,7 @@
 b
 c
 a
+% binary files and regular patch hunks
+applying patch from stdin
+foo
+045c85ba38952325e126c70962cc0f9d9077bc67 644 binary