changeset 17219:494a970f68de

obsolete: refactor writemarkers to only encode them The function is now able to write the version header as necessary. The function now yield bytes to be written to a stream. This should ease later use of this function for wireprotocol based exchanged. Prepare the public use of the writemarker by wireprotocol function.
author Pierre-Yves.David@ens-lyon.org
date Thu, 12 Jul 2012 19:58:07 +0200
parents b8661d7c940f
children bdac214a4705
files mercurial/obsolete.py
diffstat 1 files changed, 10 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/obsolete.py	Sat Jul 14 18:29:46 2012 +0200
+++ b/mercurial/obsolete.py	Thu Jul 12 19:58:07 2012 +0200
@@ -203,10 +203,9 @@
                 f.seek(0, 2) # os.SEEK_END
                 offset = f.tell()
                 transaction.add('obsstore', offset)
-                if offset == 0:
-                    # new file add version header
-                    f.write(_pack('>B', _fmversion))
-                _writemarkers(f.write, [marker])
+                # offset == 0: new file - add the version header
+                for bytes in _encodemarkers([marker], offset == 0):
+                    f.write(bytes)
             finally:
                 # XXX: f.close() == filecache invalidation == obsstore rebuilt.
                 # call 'filecacheentry.refresh()'  here
@@ -229,25 +228,26 @@
         for suc in sucs:
             self.successors.setdefault(suc, set()).add(marker)
 
-def _writemarkers(write, markers):
+def _encodemarkers(markers, addheader=False):
     # Kept separate from flushmarkers(), it will be reused for
     # markers exchange.
+    if addheader:
+        yield _pack('>B', _fmversion)
     for marker in markers:
         pre, sucs, flags, metadata = marker
         nbsuc = len(sucs)
         format = _fmfixed + (_fmnode * nbsuc)
         data = [nbsuc, len(metadata), flags, pre]
         data.extend(sucs)
-        write(_pack(format, *data))
-        write(metadata)
+        yield _pack(format, *data)
+        yield metadata
 
 def listmarkers(repo):
     """List markers over pushkey"""
     if not repo.obsstore:
         return {}
-    data = [_pack('>B', _fmversion)]
-    _writemarkers(data.append, repo.obsstore)
-    return {'dump': base85.b85encode(''.join(data))}
+    markers = _encodemarkers(repo.obsstore, True)
+    return {'dump': base85.b85encode(''.join(markers))}
 
 def pushmarker(repo, key, old, new):
     """Push markers over pushkey"""