[PATCH] diffstat: fix parsing of filenames with spaces

Gastón Kleiman gaston.kleiman at gmail.com
Fri Feb 4 13:36:46 CST 2011


# HG changeset patch
# User Gastón Kleiman <gaston.kleiman at gmail.com>
# Date 1296847934 10800
# Branch stable
# Node ID 8a7df1a5ea15881fb0d7188dc11aa5b7dd2936bc
# Parent  a939f08fae9c4a51cc9bf4b3d9c512703856df84
diffstat: fix parsing of filenames with spaces

The patch changes the output of "hg diff --stat" when one file whose filename
has spaces has changed, making it get the full filename instead of just the
substring between the last space and the end of the filename.

It also changes the diffstat generated by "hg email -d" when one of the commit
messages starts with "diff". Because of the regex used to parse the filename,
the diffstat generated by "hg email -d" will still be not correct if a commit
message starts with "diff -r ".

Before the patch Mercurial has the following behavior:

$ echo "foobar">"file with spaces"
$ hg add "file with spaces"
$ hg diff --stat
 spaces |  1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
$ hg diff --git --stat
 file with spaces |  1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

After the patch:

$ echo "foobar">"file with spaces"
$ hg add "file with spaces"
$ hg diff --stat
 file with spaces |  1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
$ hg diff --git --stat
 file with spaces |  1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

Before the patch:

$ hg add mercurial/patch.py tests/tests-diffstat.t
$ hg commit -m "diffstat: fix parsing of filenames"

$ hg email -d --test tip
This patch series consists of 1 patches.

diffstat: fix parsing of filenames
[...]

 filenames             |    0
 mercurial/patch.py    |    6 ++++--
 tests/test-diffstat.t |   17 +++++++++++++++++
 3 files changed, 21 insertions(+), 2 deletions(-)
[...]

After the patch:

$ hg email -d --test tip
This patch series consists of 1 patches.

diffstat: fix parsing of filenames

[...]

 mercurial/patch.py    |    6 ++++--
 tests/test-diffstat.t |   17 +++++++++++++++++
 3 files changed, 21 insertions(+), 2 deletions(-)
[...]

diff -r a939f08fae9c -r 8a7df1a5ea15 mercurial/patch.py
--- a/mercurial/patch.py	Sat Jan 29 23:23:24 2011 +0900
+++ b/mercurial/patch.py	Fri Feb 04 16:32:14 2011 -0300
@@ -1537,6 +1537,8 @@
                 yield text
 
 def diffstatdata(lines):
+    diffre = re.compile('^diff .*-r [a-z0-9]+\s(.*)$')
+
     filename, adds, removes = None, 0, 0
     for line in lines:
         if line.startswith('diff'):
@@ -1547,9 +1549,9 @@
             adds, removes = 0, 0
             if line.startswith('diff --git'):
                 filename = gitre.search(line).group(1)
-            else:
+            elif line.startswith('diff -r'):
                 # format: "diff -r ... -r ... filename"
-                filename = line.split(None, 5)[-1]
+                filename = diffre.search(line).group(1)
         elif line.startswith('+') and not line.startswith('+++'):
             adds += 1
         elif line.startswith('-') and not line.startswith('---'):
diff -r a939f08fae9c -r 8a7df1a5ea15 tests/test-diffstat.t
--- a/tests/test-diffstat.t	Sat Jan 29 23:23:24 2011 +0900
+++ b/tests/test-diffstat.t	Fri Feb 04 16:32:14 2011 -0300
@@ -46,3 +46,20 @@
    b |  Bin 
    1 files changed, 0 insertions(+), 0 deletions(-)
 
+  $ hg ci -m createb
+
+  $ printf '\0' > "file with spaces"
+  $ hg add "file with spaces"
+
+Filename with spaces diffstat:
+
+  $ hg diff --stat
+   file with spaces |    0 
+   1 files changed, 0 insertions(+), 0 deletions(-)
+
+Filename with spaces git diffstat:
+
+  $ hg diff --stat --git
+   file with spaces |  Bin 
+   1 files changed, 0 insertions(+), 0 deletions(-)
+	


More information about the Mercurial-devel mailing list