addremove: comparing two empty files caused ZeroDivisionError
(found by Giorgos Keramidas)
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -160,9 +160,11 @@ def findrenames(repo, added=None, remove
for line in alines[x1:x2]:
equal += len(line)
- myscore = equal*2.0 / (len(aa)+len(rr))
- if myscore >= bestscore:
- bestname, bestscore = r, myscore
+ lengths = len(aa) + len(rr)
+ if lengths:
+ myscore = equal*2.0 / lengths
+ if myscore >= bestscore:
+ bestname, bestscore = r, myscore
if bestname:
yield bestname, a, bestscore
--- a/tests/test-addremove-similar
+++ b/tests/test-addremove-similar
@@ -16,6 +16,12 @@ hg addremove -s50
hg commit -m B
+echo % comparing two empty files caused ZeroDivisionError in the past
+hg update -C 0
+rm empty-file
+touch another-empty-file
+hg addremove -s50
+
cd ..
hg init rep2; cd rep2
--- a/tests/test-addremove-similar.out
+++ b/tests/test-addremove-similar.out
@@ -4,6 +4,10 @@ adding another-file
removing empty-file
removing large-file
recording removal of large-file as rename to another-file (99% similar)
+% comparing two empty files caused ZeroDivisionError in the past
+2 files updated, 0 files merged, 1 files removed, 0 files unresolved
+adding another-empty-file
+removing empty-file
adding large-file
adding tiny-file
adding small-file