comparison mercurial/patch.py @ 32320:0e29ce16ec38

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.
author Andrew Zwicky <andrew.zwicky@gmail.com>
date Wed, 17 May 2017 20:51:17 -0500
parents 4462a981e8df
children 017ad85e5ac8
comparison
equal deleted inserted replaced
32319:d3177aecac01 32320:0e29ce16ec38
2652 2652
2653 def addresult(): 2653 def addresult():
2654 if filename: 2654 if filename:
2655 results.append((filename, adds, removes, isbinary)) 2655 results.append((filename, adds, removes, isbinary))
2656 2656
2657 # inheader is used to track if a line is in the
2658 # header portion of the diff. This helps properly account
2659 # for lines that start with '--' or '++'
2660 inheader = False
2661
2657 for line in lines: 2662 for line in lines:
2658 if line.startswith('diff'): 2663 if line.startswith('diff'):
2659 addresult() 2664 addresult()
2660 # set numbers to 0 anyway when starting new file 2665 # starting a new file diff
2666 # set numbers to 0 and reset inheader
2667 inheader = True
2661 adds, removes, isbinary = 0, 0, False 2668 adds, removes, isbinary = 0, 0, False
2662 if line.startswith('diff --git a/'): 2669 if line.startswith('diff --git a/'):
2663 filename = gitre.search(line).group(2) 2670 filename = gitre.search(line).group(2)
2664 elif line.startswith('diff -r'): 2671 elif line.startswith('diff -r'):
2665 # format: "diff -r ... -r ... filename" 2672 # format: "diff -r ... -r ... filename"
2666 filename = diffre.search(line).group(1) 2673 filename = diffre.search(line).group(1)
2667 elif line.startswith('+') and not line.startswith('+++ '): 2674 elif line.startswith('@@'):
2675 inheader = False
2676 elif line.startswith('+') and not inheader:
2668 adds += 1 2677 adds += 1
2669 elif line.startswith('-') and not line.startswith('--- '): 2678 elif line.startswith('-') and not inheader:
2670 removes += 1 2679 removes += 1
2671 elif (line.startswith('GIT binary patch') or 2680 elif (line.startswith('GIT binary patch') or
2672 line.startswith('Binary file')): 2681 line.startswith('Binary file')):
2673 isbinary = True 2682 isbinary = True
2674 addresult() 2683 addresult()