[PATCH 3 of 7 🚿🍦] patchbomb: extract 'getbundle' closure in its own function

Pierre-Yves David pierre-yves.david at ens-lyon.org
Thu Nov 6 09:46:22 CST 2014


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at fb.com>
# Date 1415136837 0
#      Tue Nov 04 21:33:57 2014 +0000
# Node ID 13f98632a2921017b3e5983a0bec73aeeb79d328
# Parent  79339dac40621b463dda037eaf889252c2255120
patchbomb: extract 'getbundle' closure in its own function

Keep marching toward the promised land of simplification!

diff --git a/hgext/patchbomb.py b/hgext/patchbomb.py
--- a/hgext/patchbomb.py
+++ b/hgext/patchbomb.py
@@ -185,10 +185,35 @@ def _getpatches(repo, revs, **opts):
         output = cStringIO.StringIO()
         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')),
     ('i', 'inline', None, _('send patches as inline attachments')),
     ('', 'bcc', [], _('email addresses of blind carbon copy recipients')),
@@ -309,26 +334,10 @@ def patchbomb(ui, repo, *revs, **opts):
     outgoing = opts.get('outgoing')
     rev = opts.get('rev')
     # internal option used by pbranches
     patches = opts.get('patches')
 
-    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)
 
     if not (revs or rev or outgoing or bundle or patches):
@@ -454,11 +463,11 @@ def patchbomb(ui, repo, *revs, **opts):
               prompt(ui, 'From', ui.username()))
 
     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)))
 
     showaddrs = []
 


More information about the Mercurial-devel mailing list