Patchwork D3775: progress: make the progress helper a context manager

login
register
mail settings
Submitter phabricator
Date June 18, 2018, 7:05 a.m.
Message ID <differential-rev-PHID-DREV-n47nglg2s3yulxgl4t4b-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/32235/
State Superseded
Headers show

Comments

phabricator - June 18, 2018, 7:05 a.m.
martinvonz created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This lets us simplify the use site in streamclone.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3775

AFFECTED FILES
  mercurial/scmutil.py
  mercurial/streamclone.py

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/mercurial/streamclone.py b/mercurial/streamclone.py
--- a/mercurial/streamclone.py
+++ b/mercurial/streamclone.py
@@ -495,38 +495,35 @@ 
     progress = repo.ui.makeprogress(_('bundle'), total=totalfilesize,
                                     unit=_('bytes'))
     progress.update(0)
-    with maketempcopies() as copy:
-        try:
-            # copy is delayed until we are in the try
-            entries = [_filterfull(e, copy, vfsmap) for e in entries]
-            yield None # this release the lock on the repository
-            seen = 0
+    with maketempcopies() as copy, progress:
+        # copy is delayed until we are in the try
+        entries = [_filterfull(e, copy, vfsmap) for e in entries]
+        yield None # this release the lock on the repository
+        seen = 0
 
-            for src, name, ftype, data in entries:
-                vfs = vfsmap[src]
-                yield src
-                yield util.uvarintencode(len(name))
-                if ftype == _fileappend:
-                    fp = vfs(name)
-                    size = data
-                elif ftype == _filefull:
-                    fp = open(data, 'rb')
-                    size = util.fstat(fp).st_size
-                try:
-                    yield util.uvarintencode(size)
-                    yield name
-                    if size <= 65536:
-                        chunks = (fp.read(size),)
-                    else:
-                        chunks = util.filechunkiter(fp, limit=size)
-                    for chunk in chunks:
-                        seen += len(chunk)
-                        progress.update(seen)
-                        yield chunk
-                finally:
-                    fp.close()
-        finally:
-            progress.complete()
+        for src, name, ftype, data in entries:
+            vfs = vfsmap[src]
+            yield src
+            yield util.uvarintencode(len(name))
+            if ftype == _fileappend:
+                fp = vfs(name)
+                size = data
+            elif ftype == _filefull:
+                fp = open(data, 'rb')
+                size = util.fstat(fp).st_size
+            try:
+                yield util.uvarintencode(size)
+                yield name
+                if size <= 65536:
+                    chunks = (fp.read(size),)
+                else:
+                    chunks = util.filechunkiter(fp, limit=size)
+                for chunk in chunks:
+                    seen += len(chunk)
+                    progress.update(seen)
+                    yield chunk
+            finally:
+                fp.close()
 
 def generatev2(repo):
     """Emit content for version 2 of a streaming clone.
diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -1293,6 +1293,12 @@ 
         self.unit = unit
         self.total = total
 
+    def __enter__(self):
+        pass
+
+    def __exit__(self, exc_type, exc_value, exc_tb):
+        self.complete()
+
     def update(self, pos, item="", total=None):
         if total:
             self.total = total