changeset 4679:826659bd8053

git patches: correct handling of filenames with spaces Add a trailing TAB to the "--- filename" lines if there's a space in the file name. This allows patch(1) to work correctly. The same is done for diff --nodates. This was originally suggested by Andrei Vermel, but at the time I thought git was doing something different.
author Alexis S. L. Carvalho <alexis@cecm.usp.br>
date Fri, 22 Jun 2007 19:06:04 -0300
parents a814a5b11fff
children 059bdc8dfb9d
files mercurial/mdiff.py tests/test-git-export tests/test-git-export.out tests/test-git-import tests/test-git-import.out
diffstat 5 files changed, 37 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/mdiff.py	Fri Jun 22 14:32:54 2007 -0300
+++ b/mercurial/mdiff.py	Fri Jun 22 19:06:04 2007 -0300
@@ -50,8 +50,12 @@
 defaultopts = diffopts()
 
 def unidiff(a, ad, b, bd, fn, r=None, opts=defaultopts):
-    def datetag(date):
-        return (opts.git or opts.nodates) and '\n' or '\t%s\n' % date
+    def datetag(date, addtab=True):
+        if not opts.git and not opts.nodates:
+            return '\t%s\n' % date
+        if addtab and ' ' in fn:
+            return '\t\n'
+        return '\n'
 
     if not a and not b: return ""
     epoch = util.datestr((0, 0))
@@ -66,7 +70,7 @@
     elif not a:
         b = splitnewlines(b)
         if a is None:
-            l1 = '--- /dev/null%s' % datetag(epoch)
+            l1 = '--- /dev/null%s' % datetag(epoch, False)
         else:
             l1 = "--- %s%s" % ("a/" + fn, datetag(ad))
         l2 = "+++ %s%s" % ("b/" + fn, datetag(bd))
@@ -76,7 +80,7 @@
         a = splitnewlines(a)
         l1 = "--- %s%s" % ("a/" + fn, datetag(ad))
         if b is None:
-            l2 = '+++ /dev/null%s' % datetag(epoch)
+            l2 = '+++ /dev/null%s' % datetag(epoch, False)
         else:
             l2 = "+++ %s%s" % ("b/" + fn, datetag(bd))
         l3 = "@@ -1,%d +0,0 @@\n" % len(a)
--- a/tests/test-git-export	Fri Jun 22 14:32:54 2007 -0300
+++ b/tests/test-git-export	Fri Jun 22 19:06:04 2007 -0300
@@ -140,3 +140,9 @@
 hg ci -m 'multiple renames/copies'
 hg diff --git -r -2 -r -1
 
+echo '% there should be a trailing TAB if there are spaces in the file name'
+echo foo > 'with spaces'
+hg add 'with spaces'
+hg diff --git
+hg ci -m 'add filename with spaces'
+
--- a/tests/test-git-export.out	Fri Jun 22 14:32:54 2007 -0300
+++ b/tests/test-git-export.out	Fri Jun 22 19:06:04 2007 -0300
@@ -145,3 +145,10 @@
 diff --git a/brand-new2 b/brand-new3-2
 copy from brand-new2
 copy to brand-new3-2
+% there should be a trailing TAB if there are spaces in the file name
+diff --git a/with spaces b/with spaces
+new file mode 100644
+--- /dev/null
++++ b/with spaces	
+@@ -0,0 +1,1 @@
++foo
--- a/tests/test-git-import	Fri Jun 22 14:32:54 2007 -0300
+++ b/tests/test-git-import	Fri Jun 22 19:06:04 2007 -0300
@@ -181,3 +181,16 @@
 
 EOF
 hg manifest --debug | grep mbinary
+
+echo % filenames with spaces
+hg import -m spaces - <<EOF
+diff --git a/foo bar b/foo bar
+new file mode 100644
+index 0000000..257cc56
+--- /dev/null
++++ b/foo bar	
+@@ -0,0 +1 @@
++foo
+EOF
+cat "foo bar"
+
--- a/tests/test-git-import.out	Fri Jun 22 14:32:54 2007 -0300
+++ b/tests/test-git-import.out	Fri Jun 22 19:06:04 2007 -0300
@@ -60,3 +60,6 @@
 applying patch from stdin
 045c85ba38952325e126c70962cc0f9d9077bc67 644 mbinary1
 a874b471193996e7cb034bb301cac7bdaf3e3f46 644 mbinary2
+% filenames with spaces
+applying patch from stdin
+foo