[PATCH 2 of 3] util: implement a deterministic __repr__ on sortdict

Gregory Szorc gregory.szorc at gmail.com
Sun Jul 17 18:18:22 EDT 2016


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1468793430 25200
#      Sun Jul 17 15:10:30 2016 -0700
# Node ID ae56588f5d45ba9443a96c612c70fbfb1f4042df
# Parent  3987b98844077a160447a9280f23b4fb47ef2b79
util: implement a deterministic __repr__ on sortdict

`hg debugbundle` is calling repr() on bundle2 part params, which are
now util.sortdict instances. Unfortunately, repr() doesn't appear
to be deterministic for util.sortdict. So, we implement one.

We include the type name because that's the common convention for
__repr__ implementations. Having the type name in `hg debugbundle`
is a bit ugly. But it's a debug command and I don't care enough to
fix it.

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -522,16 +522,20 @@ class sortdict(dict):
     def iterkeys(self):
         return self._list.__iter__()
     def iteritems(self):
         for k in self._list:
             yield k, self[k]
     def insert(self, index, key, val):
         self._list.insert(index, key)
         dict.__setitem__(self, key, val)
+    def __repr__(self):
+        if not self:
+            return '%s()' % self.__class__.__name__
+        return '%s(%r)' % (self.__class__.__name__, self.items())
 
 class _lrucachenode(object):
     """A node in a doubly linked list.
 
     Holds a reference to nodes on either side as well as a key-value
     pair for the dictionary entry.
     """
     __slots__ = ('next', 'prev', 'key', 'value')
diff --git a/tests/test-bundle-type.t b/tests/test-bundle-type.t
--- a/tests/test-bundle-type.t
+++ b/tests/test-bundle-type.t
@@ -47,53 +47,53 @@ test bundle types
   >   echo
   >   cd ..
   > done
   % test bundle type None
   searching for changes
   1 changesets found
   HG20\x00\x00 (esc)
   Stream params: {}
-  changegroup -- "{'version': '02'}"
+  changegroup -- "sortdict([('version', '02')])"
       c35a0f9217e65d1fdb90c936ffa7dbe679f83ddf
   none-v2
   
   % test bundle type bzip2
   searching for changes
   1 changesets found
   HG20\x00\x00 (esc)
-  Stream params: {'Compression': 'BZ'}
-  changegroup -- "{'version': '02'}"
+  Stream params: sortdict([('Compression', 'BZ')])
+  changegroup -- "sortdict([('version', '02')])"
       c35a0f9217e65d1fdb90c936ffa7dbe679f83ddf
   bzip2-v2
   
   % test bundle type gzip
   searching for changes
   1 changesets found
   HG20\x00\x00 (esc)
-  Stream params: {'Compression': 'GZ'}
-  changegroup -- "{'version': '02'}"
+  Stream params: sortdict([('Compression', 'GZ')])
+  changegroup -- "sortdict([('version', '02')])"
       c35a0f9217e65d1fdb90c936ffa7dbe679f83ddf
   gzip-v2
   
   % test bundle type none-v2
   searching for changes
   1 changesets found
   HG20\x00\x00 (esc)
   Stream params: {}
-  changegroup -- "{'version': '02'}"
+  changegroup -- "sortdict([('version', '02')])"
       c35a0f9217e65d1fdb90c936ffa7dbe679f83ddf
   none-v2
   
   % test bundle type v2
   searching for changes
   1 changesets found
   HG20\x00\x00 (esc)
-  Stream params: {'Compression': 'BZ'}
-  changegroup -- "{'version': '02'}"
+  Stream params: sortdict([('Compression', 'BZ')])
+  changegroup -- "sortdict([('version', '02')])"
       c35a0f9217e65d1fdb90c936ffa7dbe679f83ddf
   bzip2-v2
   
   % test bundle type v1
   searching for changes
   1 changesets found
   HG10BZ
   c35a0f9217e65d1fdb90c936ffa7dbe679f83ddf
diff --git a/tests/test-bundle2-format.t b/tests/test-bundle2-format.t
--- a/tests/test-bundle2-format.t
+++ b/tests/test-bundle2-format.t
@@ -985,17 +985,17 @@ Support for changegroup
   05f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
   0600: 00 00 00 00 00 00 00 00 00 00 00 02 de 42 19 6e |.............B.n|
   0610: be e4 2e f2 84 b6 78 0a 87 cd c9 6e 8e aa b6 00 |......x....n....|
   0620: 00 00 00 00 00 00 00 00 00 00 02 48 0a 00 00 00 |...........H....|
   0630: 00 00 00 00 00 00 00 00 00 00 00 00 00          |.............|
 
   $ hg debugbundle ../rev.hg2
   Stream params: {}
-  changegroup -- '{}'
+  changegroup -- 'sortdict()'
       32af7686d403cf45b5d95f2d70cebea587ac806a
       9520eea781bcca16c1e15acc0ba14335a0e8e5ba
       eea13746799a9e0bfd88f29d3c2e9dc9389f524f
       02de42196ebee42ef284b6780a87cdc96e8eaab6
   $ hg unbundle ../rev.hg2
   adding changesets
   adding manifests
   adding file changes
@@ -1113,18 +1113,18 @@ Simple case where it just work: GZ
   0310: 8e 5e 3c b9 b8 f8 48 6a 76 46 a7 1a b6 dd 5b 51 |.^<...HjvF....[Q|
   0320: 5e 19 1d 59 12 c6 32 89 02 9a c0 8f 4f b8 0a ba |^..Y..2.....O...|
   0330: 5e ec 58 37 44 a3 2f dd 33 ed c9 d3 dd c7 22 1b |^.X7D./.3.....".|
   0340: 2f d4 94 8e 95 3f 77 a7 ae 6e f3 32 8d bb 4a 4c |/....?w..n.2..JL|
   0350: b8 0a 5a 43 34 3a b3 3a d6 77 ff 5c b6 fa ad f9 |..ZC4:.:.w.\....|
   0360: db fb 6a 33 df c1 7d 99 cf ef d4 d5 6d da 77 7c |..j3..}.....m.w||
   0370: 3b 19 fd af c5 3f f1 60 c3 17                   |;....?.`..|
   $ hg debugbundle ../rev.hg2.bz
-  Stream params: {'Compression': 'GZ'}
-  changegroup -- '{}'
+  Stream params: sortdict([('Compression', 'GZ')])
+  changegroup -- 'sortdict()'
       32af7686d403cf45b5d95f2d70cebea587ac806a
       9520eea781bcca16c1e15acc0ba14335a0e8e5ba
       eea13746799a9e0bfd88f29d3c2e9dc9389f524f
       02de42196ebee42ef284b6780a87cdc96e8eaab6
   $ hg unbundle ../rev.hg2.bz
   adding changesets
   adding manifests
   adding file changes
@@ -1200,18 +1200,18 @@ Simple case where it just work: BZ
   03d0: 84 36 2e 16 1f 0a f7 4e 8f eb df 01 2d c2 79 0b |.6.....N....-.y.|
   03e0: f7 24 ea 0d e8 59 86 51 6e 1c 30 a3 ad 2f ee 8c |.$...Y.Qn.0../..|
   03f0: 90 c8 84 d5 e8 34 c1 95 b2 c9 f6 4d 87 1c 7d 19 |.....4.....M..}.|
   0400: d6 41 58 56 7a e0 6c ba 10 c7 e8 33 39 36 96 e7 |.AXVz.l....396..|
   0410: d2 f9 59 9a 08 95 48 38 e7 0b b7 0a 24 67 c4 39 |..Y...H8....$g.9|
   0420: 8b 43 88 57 9c 01 f5 61 b5 e1 27 41 7e af 83 fe |.C.W...a..'A~...|
   0430: 2e e4 8a 70 a1 21 46 96 30 7a                   |...p.!F.0z|
   $ hg debugbundle ../rev.hg2.bz
-  Stream params: {'Compression': 'BZ'}
-  changegroup -- '{}'
+  Stream params: sortdict([('Compression', 'BZ')])
+  changegroup -- 'sortdict()'
       32af7686d403cf45b5d95f2d70cebea587ac806a
       9520eea781bcca16c1e15acc0ba14335a0e8e5ba
       eea13746799a9e0bfd88f29d3c2e9dc9389f524f
       02de42196ebee42ef284b6780a87cdc96e8eaab6
   $ hg unbundle ../rev.hg2.bz
   adding changesets
   adding manifests
   adding file changes
diff --git a/tests/test-debugbundle.t b/tests/test-debugbundle.t
--- a/tests/test-debugbundle.t
+++ b/tests/test-debugbundle.t
@@ -26,17 +26,17 @@ Terse output:
   $ hg debugbundle bundle.hg
   0e067c57feba1a5694ca4844f05588bb1bf82342
   991a3460af53952d10ec8a295d3d2cc2e5fa9690
 
 Terse output:
 
   $ hg debugbundle bundle2.hg
   Stream params: {}
-  changegroup -- "{'version': '02'}"
+  changegroup -- "sortdict([('version', '02')])"
       0e067c57feba1a5694ca4844f05588bb1bf82342
       991a3460af53952d10ec8a295d3d2cc2e5fa9690
 
 Verbose output:
 
   $ hg debugbundle --all bundle.hg
   format: id, p1, p2, cset, delta base, len(delta)
   
@@ -51,17 +51,17 @@ Verbose output:
   b
   b80de5d138758541c5f05265ad144ab9fa86d1db 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 0e067c57feba1a5694ca4844f05588bb1bf82342 0000000000000000000000000000000000000000 0
   
   c
   b80de5d138758541c5f05265ad144ab9fa86d1db 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 991a3460af53952d10ec8a295d3d2cc2e5fa9690 0000000000000000000000000000000000000000 0
 
   $ hg debugbundle --all bundle2.hg
   Stream params: {}
-  changegroup -- "{'version': '02'}"
+  changegroup -- "sortdict([('version', '02')])"
       format: id, p1, p2, cset, delta base, len(delta)
   
       changelog
       0e067c57feba1a5694ca4844f05588bb1bf82342 3903775176ed42b1458a6281db4a0ccf4d9f287a 0000000000000000000000000000000000000000 0e067c57feba1a5694ca4844f05588bb1bf82342 3903775176ed42b1458a6281db4a0ccf4d9f287a 80
       991a3460af53952d10ec8a295d3d2cc2e5fa9690 0e067c57feba1a5694ca4844f05588bb1bf82342 0000000000000000000000000000000000000000 991a3460af53952d10ec8a295d3d2cc2e5fa9690 0e067c57feba1a5694ca4844f05588bb1bf82342 80
   
       manifest
       686dbf0aeca417636fa26a9121c681eabbb15a20 8515d4bfda768e04af4c13a69a72e28c7effbea7 0000000000000000000000000000000000000000 0e067c57feba1a5694ca4844f05588bb1bf82342 8515d4bfda768e04af4c13a69a72e28c7effbea7 55
diff --git a/tests/test-generaldelta.t b/tests/test-generaldelta.t
--- a/tests/test-generaldelta.t
+++ b/tests/test-generaldelta.t
@@ -149,13 +149,13 @@ Test format.aggressivemergedeltas
        1        59      61      0       1 315c023f341d 000000000000 000000000000
        2       120      62      0       2 2ab389a983eb 315c023f341d 8dde941edb6e
 
 Test that strip bundle use bundle2
   $ hg --config extensions.strip= strip .
   0 files updated, 0 files merged, 5 files removed, 0 files unresolved
   saved backup bundle to $TESTTMP/aggressive/.hg/strip-backup/1c5d4dc9a8b8-6c68e60c-backup.hg (glob)
   $ hg debugbundle .hg/strip-backup/*
-  Stream params: {'Compression': 'BZ'}
-  changegroup -- "{'version': '02'}"
+  Stream params: sortdict([('Compression', 'BZ')])
+  changegroup -- "sortdict([('version', '02')])"
       1c5d4dc9a8b8d6e1750966d343e94db665e7a1e9
 
   $ cd ..
diff --git a/tests/test-getbundle.t b/tests/test-getbundle.t
--- a/tests/test-getbundle.t
+++ b/tests/test-getbundle.t
@@ -165,17 +165,17 @@ Get branch and merge:
   8365676dbab05860ce0d9110f2af51368b961bbd
   0b2f73f04880d9cb6a5cd8a757f0db0ad01e32c3
 
 = Test bundle2 =
 
   $ hg debuggetbundle repo bundle -t bundle2
   $ hg debugbundle bundle
   Stream params: {}
-  changegroup -- "{'version': '01'}"
+  changegroup -- "sortdict([('version', '01')])"
       7704483d56b2a7b5db54dcee7c62378ac629b348
       29a4d1f17bd3f0779ca0525bebb1cfb51067c738
       713346a995c363120712aed1aee7e04afd867638
       d5f6e1ea452285324836a49d7d3c2a63cfed1d31
       ff42371d57168345fdf1a3aac66a51f6a45d41d2
       bac16991d12ff45f9dc43c52da1946dfadb83e80
       6621d79f61b23ec74cf4b69464343d9e0980ec8b
       8931463777131cd73923e560b760061f2aa8a4bc
diff --git a/tests/test-shelve.t b/tests/test-shelve.t
--- a/tests/test-shelve.t
+++ b/tests/test-shelve.t
@@ -1039,18 +1039,18 @@ with general delta
   6 files updated, 0 files merged, 0 files removed, 0 files unresolved
   $ cd bundle2
   $ echo babar > jungle
   $ hg add jungle
   $ hg shelve
   shelved as default
   0 files updated, 0 files merged, 1 files removed, 0 files unresolved
   $ hg debugbundle .hg/shelved/*.hg
-  Stream params: {'Compression': 'BZ'}
-  changegroup -- "{'version': '02'}"
+  Stream params: sortdict([('Compression', 'BZ')])
+  changegroup -- "sortdict([('version', '02')])"
       45993d65fe9dc3c6d8764b9c3b07fa831ee7d92d
   $ cd ..
 
 Test visibility of in-memory changes inside transaction to external hook
 ------------------------------------------------------------------------
 
   $ cd repo
 
diff --git a/tests/test-strip.t b/tests/test-strip.t
--- a/tests/test-strip.t
+++ b/tests/test-strip.t
@@ -205,18 +205,18 @@
   saved backup bundle to $TESTTMP/test/.hg/strip-backup/264128213d29-0b39d6bf-backup.hg (glob)
   $ hg parents
   changeset:   1:ef3a871183d7
   user:        test
   date:        Thu Jan 01 00:00:00 1970 +0000
   summary:     b
   
   $ hg debugbundle .hg/strip-backup/*
-  Stream params: {'Compression': 'BZ'}
-  changegroup -- "{'version': '02'}"
+  Stream params: sortdict([('Compression', 'BZ')])
+  changegroup -- "sortdict([('version', '02')])"
       264128213d290d868c54642d13aeaa3675551a78
   $ hg pull .hg/strip-backup/*
   pulling from .hg/strip-backup/264128213d29-0b39d6bf-backup.hg
   searching for changes
   adding changesets
   adding manifests
   adding file changes
   added 1 changesets with 0 changes to 0 files (+1 heads)


More information about the Mercurial-devel mailing list