comparison mercurial/patch.py @ 15363:628a4a9e411d stable

diffstat: be more picky when marking file as 'binary' (issue2816) The 'Bin' marker was added to every changed file for which we could not find any diff changes. This included binary files but also copy/renames and mode changes. Since Mercurial regular diff format emits a 'Binary file XXX has changed' line when fed with binary files, we use that and the usual git marker to tell them from other cases. In particular, new empty files are no longer reported as binary. Still, this fix is not complete since copy/renames/mode changes are now reported as '0' lines changes, instead of 'Bin'.
author Patrick Mezard <pmezard@gmail.com>
date Mon, 24 Oct 2011 13:41:19 +0200
parents 2c4fdee4d1a8
children 2b1ec74c961f
comparison
equal deleted inserted replaced
15362:60f93ddd61fd 15363:628a4a9e411d
1785 1785
1786 def diffstatdata(lines): 1786 def diffstatdata(lines):
1787 diffre = re.compile('^diff .*-r [a-z0-9]+\s(.*)$') 1787 diffre = re.compile('^diff .*-r [a-z0-9]+\s(.*)$')
1788 1788
1789 results = [] 1789 results = []
1790 filename, adds, removes = None, 0, 0 1790 filename, adds, removes, isbinary = None, 0, 0, False
1791 1791
1792 def addresult(): 1792 def addresult():
1793 if filename: 1793 if filename:
1794 isbinary = adds == 0 and removes == 0
1795 results.append((filename, adds, removes, isbinary)) 1794 results.append((filename, adds, removes, isbinary))
1796 1795
1797 for line in lines: 1796 for line in lines:
1798 if line.startswith('diff'): 1797 if line.startswith('diff'):
1799 addresult() 1798 addresult()
1800 # set numbers to 0 anyway when starting new file 1799 # set numbers to 0 anyway when starting new file
1801 adds, removes = 0, 0 1800 adds, removes, isbinary = 0, 0, False
1802 if line.startswith('diff --git'): 1801 if line.startswith('diff --git'):
1803 filename = gitre.search(line).group(1) 1802 filename = gitre.search(line).group(1)
1804 elif line.startswith('diff -r'): 1803 elif line.startswith('diff -r'):
1805 # format: "diff -r ... -r ... filename" 1804 # format: "diff -r ... -r ... filename"
1806 filename = diffre.search(line).group(1) 1805 filename = diffre.search(line).group(1)
1807 elif line.startswith('+') and not line.startswith('+++'): 1806 elif line.startswith('+') and not line.startswith('+++'):
1808 adds += 1 1807 adds += 1
1809 elif line.startswith('-') and not line.startswith('---'): 1808 elif line.startswith('-') and not line.startswith('---'):
1810 removes += 1 1809 removes += 1
1810 elif (line.startswith('GIT binary patch') or
1811 line.startswith('Binary file')):
1812 isbinary = True
1811 addresult() 1813 addresult()
1812 return results 1814 return results
1813 1815
1814 def diffstat(lines, width=80, git=False): 1816 def diffstat(lines, width=80, git=False):
1815 output = [] 1817 output = []
1830 # which isn't very useful, so always print at least one + or - 1832 # which isn't very useful, so always print at least one + or -
1831 # if there were at least some changes. 1833 # if there were at least some changes.
1832 return max(i * graphwidth // maxtotal, int(bool(i))) 1834 return max(i * graphwidth // maxtotal, int(bool(i)))
1833 1835
1834 for filename, adds, removes, isbinary in stats: 1836 for filename, adds, removes, isbinary in stats:
1835 if git and isbinary: 1837 if isbinary:
1836 count = 'Bin' 1838 count = 'Bin'
1837 else: 1839 else:
1838 count = adds + removes 1840 count = adds + removes
1839 pluses = '+' * scale(adds) 1841 pluses = '+' * scale(adds)
1840 minuses = '-' * scale(removes) 1842 minuses = '-' * scale(removes)