# HG changeset patch # User Alexis S. L. Carvalho # Date 1182549964 10800 # Node ID 826659bd8053318e2871a8ed3d3c8a2ab18a5179 # Parent a814a5b11fffb43c1b7d774422530509aa2212e8 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. diff --git a/mercurial/mdiff.py b/mercurial/mdiff.py --- a/mercurial/mdiff.py +++ b/mercurial/mdiff.py @@ -50,8 +50,12 @@ class diffopts(object): 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 @@ def unidiff(a, ad, b, bd, fn, r=None, op 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 @@ def unidiff(a, ad, b, bd, fn, r=None, op 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) diff --git a/tests/test-git-export b/tests/test-git-export --- a/tests/test-git-export +++ b/tests/test-git-export @@ -140,3 +140,9 @@ hg mv brand-new2 brand-new3-2 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' + diff --git a/tests/test-git-export.out b/tests/test-git-export.out --- a/tests/test-git-export.out +++ b/tests/test-git-export.out @@ -145,3 +145,10 @@ rename to brand-new3 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 diff --git a/tests/test-git-import b/tests/test-git-import --- a/tests/test-git-import +++ b/tests/test-git-import @@ -181,3 +181,16 @@ Mc\${NkU|\`?^000jF3jhEB EOF hg manifest --debug | grep mbinary + +echo % filenames with spaces +hg import -m spaces - <