# HG changeset patch # User Pierre-Yves David # Date 2014-11-04 21:33:57 # Node ID 6993282e5362aa7d124d0f3efba163de05509669 # Parent 79f7444520bf7d33f9012e0d4503b1622d5cba57 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 @@ -168,6 +168,31 @@ def _getpatches(repo, revs, **opts): 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 @@ def patchbomb(ui, repo, *revs, **opts): 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 @@ def patchbomb(ui, repo, *revs, **opts): 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)))