bundle2: track life cycle of parts
authorPierre-Yves David <pierre-yves.david@fb.com>
Thu, 22 May 2014 11:14:02 -0700
changeset 21601 7ff01befc7ec
parent 21600 5e08f3b65510
child 21602 cc33ae50bab3
bundle2: track life cycle of parts We introduce a ``_generated`` attribute on parts. Coming changesets will make it easier to update a part's contents after its creation. We need a way to track if the part is still open to modification or if it is currently being generated and should not be touched anymore. As a bonus, we can now detect and crash if someone manages to write bogus code to get a part generated twice.
mercurial/bundle2.py
--- a/mercurial/bundle2.py	Fri May 23 15:59:19 2014 -0700
+++ b/mercurial/bundle2.py	Thu May 22 11:14:02 2014 -0700
@@ -552,8 +552,17 @@
         self.data = data
         self.mandatoryparams = mandatoryparams
         self.advisoryparams = advisoryparams
+        # status of the part's generation:
+        # - None: not started,
+        # - False: currently generated,
+        # - True: generation done.
+        self._generated = None
 
+    # methods used to generates the bundle2 stream
     def getchunks(self):
+        if self._generated is not None:
+            raise RuntimeError('part can only be consumed once')
+        self._generated = False
         #### header
         ## parttype
         header = [_pack(_fparttypesize, len(self.type)),
@@ -591,6 +600,7 @@
             yield chunk
         # end of payload
         yield _pack(_fpayloadsize, 0)
+        self._generated = True
 
     def _payloadchunks(self):
         """yield chunks of a the part payload