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

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


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at fb.com>
# Date 1415136537 0
#      Tue Nov 04 21:28:57 2014 +0000
# Node ID 79339dac40621b463dda037eaf889252c2255120
# Parent  66beee640558544d50494f67ae281ccec34eae06
patchbomb: extract 'getpatches' 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
@@ -169,10 +169,25 @@ def _getoutgoing(repo, dest, revs):
     if not revs:
         ui.status(_("no changes found\n"))
         return []
     return [str(r) for r in revs]
 
+def _getpatches(repo, revs, **opts):
+    """return a list of patches for a list of revisions
+
+    Each patch in the list is itself a list of lines.
+    """
+    ui = repo.ui
+    prev = repo['.'].rev()
+    for r in scmutil.revrange(repo, revs):
+        if r == prev and (repo[None].files() or repo[None].deleted()):
+            ui.warn(_('warning: working directory has '
+                      'uncommitted changes\n'))
+        output = cStringIO.StringIO()
+        cmdutil.export(repo, [r], fp=output,
+                     opts=patch.diffopts(ui, opts))
+        yield output.getvalue().split('\n')
 
 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')),
@@ -294,21 +309,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 getpatches(revs):
-        prev = repo['.'].rev()
-        for r in scmutil.revrange(repo, revs):
-            if r == prev and (repo[None].files() or repo[None].deleted()):
-                ui.warn(_('warning: working directory has '
-                          'uncommitted changes\n'))
-            output = cStringIO.StringIO()
-            cmdutil.export(repo, [r], fp=output,
-                         opts=patch.diffopts(ui, opts))
-            yield output.getvalue().split('\n')
-
     def getbundle(dest):
         tmpdir = tempfile.mkdtemp(prefix='hg-email-bundle-')
         tmpfn = os.path.join(tmpdir, 'bundle')
         try:
             commands.bundle(ui, repo, tmpfn, dest, **opts)
@@ -452,11 +456,11 @@ def patchbomb(ui, repo, *revs, **opts):
     if patches:
         msgs = getpatchmsgs(patches, opts.get('patchnames'))
     elif bundle:
         msgs = getbundlemsgs(getbundle(dest))
     else:
-        msgs = getpatchmsgs(list(getpatches(revs)))
+        msgs = getpatchmsgs(list(_getpatches(repo, revs, **opts)))
 
     showaddrs = []
 
     def getaddrs(header, ask=False, default=None):
         configkey = header.lower()


More information about the Mercurial-devel mailing list