Patchwork [5,of,5] cmdutil: avoid redefining write() function in export if possible

login
register
mail settings
Submitter Augie Fackler
Date May 22, 2017, 7:09 p.m.
Message ID <9e2bdfd6fc087016bba8.1495480163@augie-macbookpro2.roam.corp.google.com>
Download mbox | patch
Permalink /patch/20840/
State Accepted
Headers show

Comments

Augie Fackler - May 22, 2017, 7:09 p.m.
# HG changeset patch
# User Augie Fackler <augie@google.com>
# Date 1495479918 14400
#      Mon May 22 15:05:18 2017 -0400
# Node ID 9e2bdfd6fc087016bba880d3e8852bb946cf7460
# Parent  7cd8268c9ad7a36ab3a54d8bddafef0d9dc1a93a
cmdutil: avoid redefining write() function in export if possible

Doing less work inside the loop just feels better to me.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -1205,11 +1205,19 @@  def export(repo, revs, fntemplate='hg-%h
     revwidth = max(len(str(rev)) for rev in revs)
     filemode = {}
 
+    write = None
+    dest = '<unnamed>'
+    if fp:
+        dest = getattr(fp, 'name', dest)
+        def write(s, **kw):
+            fp.write(s)
+    elif not fntemplate:
+        write = repo.ui.write
+
     for seqno, rev in enumerate(revs, 1):
         ctx = repo[rev]
         fo = None
-        dest = '<unnamed>'
-        if not fp and len(fntemplate) > 0:
+        if not fp and fntemplate:
             desc_lines = ctx.description().rstrip().split('\n')
             desc = desc_lines[0]    #Commit always has a first line.
             fo = makefileobj(repo, fntemplate, ctx.node(), desc=desc,
@@ -1218,12 +1226,6 @@  def export(repo, revs, fntemplate='hg-%h
             dest = fo.name
             def write(s, **kw):
                 fo.write(s)
-        elif fp:
-            dest = getattr(fp, 'name', dest)
-            def write(s, **kw):
-                fp.write(s)
-        else:
-            write = repo.ui.write
         if not dest.startswith('<'):
             repo.ui.note("%s\n" % dest)
         _exportsingle(