Split make_file back out into make_filename and make_file.
authorBryan O'Sullivan <bos@serpentine.com>
Wed, 20 Jul 2005 04:08:11 -0800
changeset 739 36edb39e8e8c
parent 738 32a97c9d8e98
child 740 d2422f10c136
Split make_file back out into make_filename and make_file. It turns out that make_filename is useful by itself.
mercurial/commands.py
--- a/mercurial/commands.py	Wed Jul 20 03:55:16 2005 -0800
+++ b/mercurial/commands.py	Wed Jul 20 04:08:11 2005 -0800
@@ -116,15 +116,8 @@
         else:
             yield spec
 
-def make_file(repo, r, pat, node=None,
-              total=None, seqno=None, revwidth=None, mode='wb'):
-    if not pat or pat == '-':
-        if 'w' in mode: return sys.stdout
-        else: return sys.stdin
-    if hasattr(pat, 'write') and 'w' in mode:
-        return pat
-    if hasattr(pat, 'read') and 'r' in mode:
-        return pat
+def make_filename(repo, r, pat, node=None,
+                  total=None, seqno=None, revwidth=None):
     node_expander = {
         'H': lambda: hg.hex(node),
         'R': lambda: str(r.rev(node)),
@@ -158,11 +151,23 @@
                 c = expander[c]()
             newname.append(c)
             i += 1
-        return open(''.join(newname), mode)
+        return ''.join(newname)
     except KeyError, inst:
         raise Abort("invalid format spec '%%%s' in output file name",
                     inst.args[0])
 
+def make_file(repo, r, pat, node=None,
+              total=None, seqno=None, revwidth=None, mode='wb'):
+    if not pat or pat == '-':
+        if 'w' in mode: return sys.stdout
+        else: return sys.stdin
+    if hasattr(pat, 'write') and 'w' in mode:
+        return pat
+    if hasattr(pat, 'read') and 'r' in mode:
+        return pat
+    return open(make_filename(repo, r, pat, node, total, seqno, revwidth),
+                mode)
+
 def dodiff(fp, ui, repo, files=None, node1=None, node2=None):
     def date(c):
         return time.asctime(time.gmtime(float(c[2].split(' ')[0])))
@@ -590,6 +595,7 @@
     fp.write("\n\n")
 
     dodiff(fp, ui, repo, None, prev, node)
+    if fp != sys.stdout: fp.close()
 
 def export(ui, repo, *changesets, **opts):
     """dump the header and diffs for one or more changesets"""