changeset 23211:6993282e5362

patchbomb: extract 'getbundle' closure in its own function Keep marching toward the promised land of simplification!
author Pierre-Yves David <pierre-yves.david@fb.com>
date Tue, 04 Nov 2014 21:33:57 +0000
parents 79f7444520bf
children 4b4eae00f9dd
files hgext/patchbomb.py
diffstat 1 files changed, 26 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- 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)))