Bug 5479 - `hg diff --stat` gets confused by lines that start with "-- " or "++ "
Summary: `hg diff --stat` gets confused by lines that start with "-- " or "++ "
Status: RESOLVED FIXED
Alias: None
Product: Mercurial
Classification: Unclassified
Component: Mercurial (show other bugs)
Version: unspecified
Hardware: All All
: wish bug
Assignee: Andrew Zwicky
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-02-05 21:06 UTC by Kyle Lippincott
Modified: 2017-05-19 20:35 UTC (History)
3 users (show)

See Also:
Python Version: ---


Attachments
test that shows the issue (as patch from test-diffstat.t) (1.99 KB, text/plain)
2017-02-05 21:06 UTC, Kyle Lippincott
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Kyle Lippincott 2017-02-05 21:06 UTC
Created attachment 1947 [details]
test that shows the issue (as patch from test-diffstat.t)

Note: possible duplicate of 1123?  That claims that diffstat was an external tool, though, so while the issue might be similar, I don't think it's a strict duplicate.

If a line in a file starts with "-- " and is changed, `hg diff --stat` doesn't count it as a deletion.  Similarly, if the new version of the line starts with "++ ", it is not counted as an addition.  This can lead to diffstat claiming 0 diffs even if there are some:

  $ hg diff --root .
  diff -r 32e517bb8958 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
This is wrong: does not count either, because both lines look like file headers.
  $ hg diff --root . --stat
   file |  0
   1 files changed, 0 insertions(+), 0 deletions(-)
Comment 1 Andrew Zwicky 2017-05-17 21:10 UTC
looks like it's caused by the starred lines in patch.py, diffstatdata function (rev 8a87bfc5bebb)

  2657	    for line in lines:
  2658	        if line.startswith('diff'):
  2659	            addresult()
  2660	            # set numbers to 0 anyway when starting new file
  2661	            adds, removes, isbinary = 0, 0, False
  2662	            if line.startswith('diff --git a/'):
  2663	                filename = gitre.search(line).group(2)
  2664	            elif line.startswith('diff -r'):
  2665	                # format: "diff -r ... -r ... filename"
  2666	                filename = diffre.search(line).group(1)
 *2667	        elif line.startswith('+') and not line.startswith('+++ '):
  2668	            adds += 1
 *2669	        elif line.startswith('-') and not line.startswith('--- '):
  2670	            removes += 1

I'm working on a resolution.
Comment 2 Andrew Zwicky 2017-05-18 10:50 UTC
patch submitted at:

https://www.mercurial-scm.org/pipermail/mercurial-devel/2017-May/098048.html

please test if this resolves the issue as expected.
Comment 3 Andrew Zwicky 2017-05-19 20:35 UTC
patched in 0e29ce16ec38