[PATCH] diffstat: properly count lines starting in '--' or '++' (issue5479)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] diffstat: properly count lines starting in '--' or '++' (issue5479)

Andrew Zwicky
# HG changeset patch
# User Andrew Zwicky <[hidden email]>
# Date 1495072277 18000
#      Wed May 17 20:51:17 2017 -0500
# Node ID 51153c6062f6c0943ca741a003bfd88feb5adfac
# Parent  8a87bfc5bebbbe0ac996ac8e047a029eb931af45
diffstat: properly count lines starting in '--' or '++' (issue5479)

Lines that start in '--' or '++' were previously not counted
as deletions or additions in diffstat, resulting in incorrect
addition/deletion counts.  The bug was present if the start
of the line, combined with the diff character resulted
in '---' or '+++'.

diffstatdata will now track, for each file, if it has moved
pas the header section by looking for a line beginning with
'@@'.  Once that has happened, lines beginning with '-'
or '+' will be counted for deletions and additions.  Once a
line beginning with 'diff' is found, the process starts over.

diff -r 8a87bfc5bebb -r 51153c6062f6 mercurial/patch.py
--- a/mercurial/patch.py Tue May 16 17:47:27 2017 -0700
+++ b/mercurial/patch.py Wed May 17 20:51:17 2017 -0500
@@ -2654,19 +2654,29 @@
         if filename:
             results.append((filename, adds, removes, isbinary))
 
+    # inheader is used to track if a line is in the
+    # header portion of the diff.  This helps properly account
+    # for lines that start with '--' or '++'
+    inheader = False
+
     for line in lines:
         if line.startswith('diff'):
+
             addresult()
-            # set numbers to 0 anyway when starting new file
+            # starting a new file diff
+            # set numbers to 0 and reset inheader
+            inheader = True
             adds, removes, isbinary = 0, 0, False
             if line.startswith('diff --git a/'):
                 filename = gitre.search(line).group(2)
             elif line.startswith('diff -r'):
                 # format: "diff -r ... -r ... filename"
                 filename = diffre.search(line).group(1)
-        elif line.startswith('+') and not line.startswith('+++ '):
+        elif line.startswith('@@'):
+            inheader = False
+        elif line.startswith('+') and not inheader:
             adds += 1
-        elif line.startswith('-') and not line.startswith('--- '):
+        elif line.startswith('-') and not inheader:
             removes += 1
         elif (line.startswith('GIT binary patch') or
               line.startswith('Binary file')):
diff -r 8a87bfc5bebb -r 51153c6062f6 tests/test-diffstat.t
--- a/tests/test-diffstat.t Tue May 16 17:47:27 2017 -0700
+++ b/tests/test-diffstat.t Wed May 17 20:51:17 2017 -0500
@@ -105,3 +105,83 @@
   $ hg diff --stat --root . -I old
 
   $ cd ..
+
+Files with lines beginning with '--' or '++' should be properly counted in diffstat
+
+  $ hg up -Cr tip
+  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ rm dir1/new
+  $ rm dir2/new
+  $ rm "file with spaces"
+  $ cat > file << EOF
+  > line 1
+  > line 2
+  > line 3
+  > EOF
+  $ hg commit -Am file
+  adding file
+
+Lines added starting with '--' should count as additions
+  $ cat > file << EOF
+  > line 1
+  > -- line 2, with dashes
+  > line 3
+  > EOF
+
+  $ hg diff --root .
+  diff -r be1569354b24 file
+  --- a/file Thu Jan 01 00:00:00 1970 +0000
+  +++ b/file * (glob)
+  @@ -1,3 +1,3 @@
+   line 1
+  -line 2
+  +-- line 2, with dashes
+   line 3
+
+  $ hg diff --root . --stat
+   file |  2 +-
+   1 files changed, 1 insertions(+), 1 deletions(-)
+
+Lines changed starting with '--' should count as deletions
+  $ hg commit -m filev2
+  $ cat > file << EOF
+  > line 1
+  > -- line 2, with dashes, changed again
+  > line 3
+  > EOF
+
+  $ hg diff --root .
+  diff -r 160f7c034df6 file
+  --- a/file Thu Jan 01 00:00:00 1970 +0000
+  +++ b/file * (glob)
+  @@ -1,3 +1,3 @@
+   line 1
+  --- line 2, with dashes
+  +-- line 2, with dashes, changed again
+   line 3
+
+  $ hg diff --root . --stat
+   file |  2 +-
+   1 files changed, 1 insertions(+), 1 deletions(-)
+
+Lines changed starting with '--' should count as deletions
+and starting with '++' should count as additions
+  $ cat > file << EOF
+  > line 1
+  > ++ line 2, switched dashes to plusses
+  > line 3
+  > EOF
+
+  $ hg diff --root .
+  diff -r 160f7c034df6 file
+  --- a/file Thu Jan 01 00:00:00 1970 +0000
+  +++ b/file * (glob)
+  @@ -1,3 +1,3 @@
+   line 1
+  --- line 2, with dashes
+  +++ line 2, switched dashes to plusses
+   line 3
+
+  $ hg diff --root . --stat
+   file |  2 +-
+   1 files changed, 1 insertions(+), 1 deletions(-)
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] diffstat: properly count lines starting in '--' or '++' (issue5479)

Yuya Nishihara
On Wed, 17 May 2017 22:35:47 -0500, Andrew Zwicky wrote:
> # HG changeset patch
> # User Andrew Zwicky <[hidden email]>
> # Date 1495072277 18000
> #      Wed May 17 20:51:17 2017 -0500
> # Node ID 51153c6062f6c0943ca741a003bfd88feb5adfac
> # Parent  8a87bfc5bebbbe0ac996ac8e047a029eb931af45
> diffstat: properly count lines starting in '--' or '++' (issue5479)

Nice. Queued, thanks.
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel