Patchwork [1,of,2,V2] debugdeltachain: avoid division by zero when a chain is empty

login
register
mail settings
Submitter Boris Feld
Date July 11, 2018, 12:48 p.m.
Message ID <0be6256636d19c196111.1531313314@FB-lair>
Download mbox | patch
Permalink /patch/32770/
State Accepted
Headers show

Comments

Boris Feld - July 11, 2018, 12:48 p.m.
# HG changeset patch
# User Paul Morelle <paul.morelle@octobus.net>
# Date 1529597997 -7200
#      Thu Jun 21 18:19:57 2018 +0200
# Node ID 0be6256636d19c196111d2445b3d6bd8aff5e445
# Parent  4d5fb4062f0bb159230062701461fa6cab9b539b
# EXP-Topic debugdeltachain-divbyzero
# Available At https://bitbucket.org/octobus/mercurial-devel/
#              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 0be6256636d1
debugdeltachain: avoid division by zero when a chain is empty

The two ratios chainratio and extraratio are computed using dividers
that may be zero when the file is empty.
As the denominators are integers, the limit of the ratio "just before zero" is
the numerator value itself.
If the numerator itself is zero, the ratio value is still meaningful: in both
cases, a "good" value is a low ratio, and a size of zero is the optimal case.
Yuya Nishihara - July 11, 2018, 1:58 p.m.
On Wed, 11 Jul 2018 14:48:34 +0200, Boris Feld wrote:
> # HG changeset patch
> # User Paul Morelle <paul.morelle@octobus.net>
> # Date 1529597997 -7200
> #      Thu Jun 21 18:19:57 2018 +0200
> # Node ID 0be6256636d19c196111d2445b3d6bd8aff5e445
> # Parent  4d5fb4062f0bb159230062701461fa6cab9b539b
> # EXP-Topic debugdeltachain-divbyzero
> # Available At https://bitbucket.org/octobus/mercurial-devel/
> #              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 0be6256636d1
> debugdeltachain: avoid division by zero when a chain is empty

Queued these. Thanks for adding tests for ZeroDivisionError.

Patch

diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py
--- a/mercurial/debugcommands.py
+++ b/mercurial/debugcommands.py
@@ -678,8 +678,15 @@  def debugdeltachain(ui, repo, file_=None
         except IndexError:
             prevrev = -1
 
-        chainratio = float(chainsize) / float(uncomp)
-        extraratio = float(extradist) / float(chainsize)
+        if uncomp != 0:
+            chainratio = float(chainsize) / float(uncomp)
+        else:
+            chainratio = chainsize
+
+        if chainsize != 0:
+            extraratio = float(extradist) / float(chainsize)
+        else:
+            extraratio = extradist
 
         fm.startitem()
         fm.write('rev chainid chainlen prevrev deltatype compsize '
diff --git a/tests/test-generaldelta.t b/tests/test-generaldelta.t
--- a/tests/test-generaldelta.t
+++ b/tests/test-generaldelta.t
@@ -172,6 +172,44 @@  test maxdeltachainspan
   $ hg init source-repo
   $ cd source-repo
   $ hg debugbuilddag --new-file '.+5:brancha$.+11:branchb$.+30:branchc<brancha+2<branchb+2'
+# add an empty revision somewhere
+  $ hg up tip
+  14 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ hg rm .
+  removing nf10
+  removing nf11
+  removing nf12
+  removing nf13
+  removing nf14
+  removing nf15
+  removing nf16
+  removing nf17
+  removing nf51
+  removing nf52
+  removing nf6
+  removing nf7
+  removing nf8
+  removing nf9
+  $ hg commit -m 'empty all'
+  $ hg revert --all --rev 'p1(.)'
+  adding nf10
+  adding nf11
+  adding nf12
+  adding nf13
+  adding nf14
+  adding nf15
+  adding nf16
+  adding nf17
+  adding nf51
+  adding nf52
+  adding nf6
+  adding nf7
+  adding nf8
+  adding nf9
+  $ hg commit -m 'restore all'
+  $ hg up null
+  0 files updated, 0 files merged, 14 files removed, 0 files unresolved
+  $ 
   $ cd ..
   $ hg -R source-repo debugdeltachain -m
       rev  chain# chainlen     prev   delta       size    rawsize  chainsize     ratio   lindist extradist extraratio
@@ -228,13 +266,15 @@  test maxdeltachainspan
        50       4        2       49      p1         58        362        255   0.70442       255         0    0.00000
        51       4        3       50    prev        356        594        611   1.02862       611         0    0.00000
        52       4        4       51      p1         58        640        669   1.04531       669         0    0.00000
+       53       5        1       -1    base          0          0          0   0.00000         0         0    0.00000
+       54       5        2       53      p1        376        640        376   0.58750       376         0    0.00000
   $ hg clone --pull source-repo --config experimental.maxdeltachainspan=2800 relax-chain --config format.generaldelta=yes
   requesting all changes
   adding changesets
   adding manifests
   adding file changes
-  added 53 changesets with 53 changes to 53 files (+2 heads)
-  new changesets 61246295ee1e:99cae3713489
+  added 55 changesets with 53 changes to 53 files (+2 heads)
+  new changesets 61246295ee1e:c930ac4a5b32
   updating to branch default
   14 files updated, 0 files merged, 0 files removed, 0 files unresolved
   $ hg -R relax-chain debugdeltachain -m
@@ -292,13 +332,15 @@  test maxdeltachainspan
        50       4        2       49      p1         58        362        255   0.70442       255         0    0.00000
        51       2       13       17      p1         58        594        739   1.24411      2781      2042    2.76319
        52       5        1       -1    base        369        640        369   0.57656       369         0    0.00000
+       53       6        1       -1    base          0          0          0   0.00000         0         0    0.00000
+       54       6        2       53      p1        376        640        376   0.58750       376         0    0.00000
   $ hg clone --pull source-repo --config experimental.maxdeltachainspan=0 noconst-chain --config format.generaldelta=yes
   requesting all changes
   adding changesets
   adding manifests
   adding file changes
-  added 53 changesets with 53 changes to 53 files (+2 heads)
-  new changesets 61246295ee1e:99cae3713489
+  added 55 changesets with 53 changes to 53 files (+2 heads)
+  new changesets 61246295ee1e:c930ac4a5b32
   updating to branch default
   14 files updated, 0 files merged, 0 files removed, 0 files unresolved
   $ hg -R noconst-chain debugdeltachain -m
@@ -356,3 +398,5 @@  test maxdeltachainspan
        50       1        8       49      p1         58        362        447   1.23481      2915      2468    5.52125
        51       2       13       17      p1         58        594        739   1.24411      2642      1903    2.57510
        52       2       14       51      p1         58        640        797   1.24531      2700      1903    2.38770
+       53       4        1       -1    base          0          0          0   0.00000         0         0    0.00000
+       54       4        2       53      p1        376        640        376   0.58750       376         0    0.00000