patchbomb: extract 'getbundle' closure in its own function
authorPierre-Yves David <pierre-yves.david@fb.com>
Tue, 04 Nov 2014 21:33:57 +0000
changeset 23211 6993282e5362
parent 23210 79f7444520bf
child 23212 4b4eae00f9dd
patchbomb: extract 'getbundle' closure in its own function Keep marching toward the promised land of simplification!
hgext/patchbomb.py
--- a/hgext/patchbomb.py	Tue Nov 04 21:28:57 2014 +0000
+++ b/hgext/patchbomb.py	Tue Nov 04 21:33:57 2014 +0000
@@ -168,6 +168,31 @@
         cmdutil.export(repo, [r], fp=output,
                      opts=patch.diffopts(ui, opts))
         yield output.getvalue().split('\n')
+def _getbundle(repo, dest, **opts):
+    """return a bundle containing changesets missing in "dest"
+
+    The `opts` keyword-arguments are the same as the one accepted by the
+    `bundle` command.
+
+    The bundle is a returned as a single in-memory binary blob.
+    """
+    ui = repo.ui
+    tmpdir = tempfile.mkdtemp(prefix='hg-email-bundle-')
+    tmpfn = os.path.join(tmpdir, 'bundle')
+    try:
+        commands.bundle(ui, repo, tmpfn, dest, **opts)
+        fp = open(tmpfn, 'rb')
+        data = fp.read()
+        fp.close()
+        return data
+    finally:
+        try:
+            os.unlink(tmpfn)
+        except OSError:
+            pass
+        os.rmdir(tmpdir)
+
+
 emailopts = [
     ('', 'body', None, _('send patches as inline message text (default)')),
     ('a', 'attach', None, _('send patches as attachments')),
@@ -307,22 +332,6 @@
             return []
         return [str(r) for r in revs]
 
-    def getbundle(dest):
-        tmpdir = tempfile.mkdtemp(prefix='hg-email-bundle-')
-        tmpfn = os.path.join(tmpdir, 'bundle')
-        try:
-            commands.bundle(ui, repo, tmpfn, dest, **opts)
-            fp = open(tmpfn, 'rb')
-            data = fp.read()
-            fp.close()
-            return data
-        finally:
-            try:
-                os.unlink(tmpfn)
-            except OSError:
-                pass
-            os.rmdir(tmpdir)
-
     if not (opts.get('test') or mbox):
         # really sending
         mail.validateconfig(ui)
@@ -452,7 +461,7 @@
     if patches:
         msgs = getpatchmsgs(patches, opts.get('patchnames'))
     elif bundle:
-        msgs = getbundlemsgs(getbundle(dest))
+        msgs = getbundlemsgs(_getbundle(repo, dest, **opts))
     else:
         msgs = getpatchmsgs(list(_getpatches(repo, revs, **opts)))