[PATCH 2 of 5] export: do not print '<fdopen>' as an output filename

Yuya Nishihara yuya at tcha.org
Thu Dec 17 08:09:22 CST 2015


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1450003666 -32400
#      Sun Dec 13 19:47:46 2015 +0900
# Node ID f96392009681a7702d6949884415b3c44c365952
# Parent  2277a47b0044907d346eefb98891c73aa43db3ba
export: do not print '<fdopen>' as an output filename

Because makefileobj() duplicates or wraps stdout, "fp != sys.stdout" didn't
work correctly. Python doc states that special file objects are named in the
form '<...>', and absolute filenames should never start with '<', we can
ignore names start with '<'. We can't test fp.fileno() because fp may be a
command-server channel.

https://docs.python.org/2.7/library/stdtypes.html#file.name

In the test output, "exporting patch:" line is printed after patch content.
This is caused by fdopen() and will be fixed by the subsequent patch.

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -1055,7 +1055,7 @@ def export(repo, revs, template='hg-%h.p
                              modemap=filemode)
             if fp != template:
                 shouldclose = True
-        if fp and fp != sys.stdout and util.safehasattr(fp, 'name'):
+        if fp and not getattr(fp, 'name', '<unnamed>').startswith('<'):
             repo.ui.note("%s\n" % fp.name)
 
         if not fp:
diff --git a/tests/test-export.t b/tests/test-export.t
--- a/tests/test-export.t
+++ b/tests/test-export.t
@@ -137,6 +137,25 @@ Exporting revision -2 to a file:
    foo-9
   +foo-10
 
+No filename should be printed if stdout is specified explicitly:
+
+  $ hg export -v 1 -o -
+  # HG changeset patch
+  # User test
+  # Date 0 0
+  #      Thu Jan 01 00:00:00 1970 +0000
+  # Node ID d1c9656e973cfb5aebd5499bbd2cb350e3b12266
+  # Parent  871558de6af2e8c244222f8eea69b782c94ce3df
+  foo-1
+  
+  diff -r 871558de6af2 -r d1c9656e973c foo
+  --- a/foo	Thu Jan 01 00:00:00 1970 +0000
+  +++ b/foo	Thu Jan 01 00:00:00 1970 +0000
+  @@ -1,1 +1,2 @@
+   foo-0
+  +foo-1
+  exporting patch:
+
 Checking if only alphanumeric characters are used in the file name (%m option):
 
   $ echo "line" >> foo


More information about the Mercurial-devel mailing list