[PATCH 1 of 1 V3] add: introduce a warning message for non-portable filenames (issue2756) (BC)

Adrian Buehlmann adrian at cadifra.com
Tue Apr 19 05:43:38 CDT 2011


# HG changeset patch
# User Adrian Buehlmann <adrian at cadifra.com>
# Date 1303209773 -7200
# Node ID bd1b08994a3bb0f4d3494eb4f3ffd7cf8adfb29f
# Parent  1184bb274cb3e683e2aabb89d0ebbad322cdb2a2
add: introduce a warning message for non-portable filenames (issue2756) (BC)

On POSIX platforms, the 'add', 'addremove', 'copy' and 'rename' commands now
warn if a file has a name that can't be checked out on Windows.

Example:

  $ hg add con.xml
  warning: filename contains 'con', which is reserved on Windows: 'con.xml'
  $ hg status
  A con.xml

The file is added despite the warning.

The warning is ON by default. It can be suppressed by setting the config option
'portablefilenames' in section 'ui' to 'ignore' or 'false':

  $ hg --config ui.portablefilenames=ignore add con.xml
  $ hg sta
  A con.xml

If ui.portablefilenames is set to 'abort', then the command is aborted:

  $ hg --config ui.portablefilenames=abort add con.xml
  abort: filename contains 'con', which is reserved on Windows: 'con.xml'

On Windows, the ui.portablefilenames config setting is irrelevant and the
command is always aborted if a problematic filename is found.

diff --git a/doc/hgrc.5.txt b/doc/hgrc.5.txt
--- a/doc/hgrc.5.txt
+++ b/doc/hgrc.5.txt
@@ -910,6 +910,16 @@
     The conflict resolution program to use during a manual merge.
     For more information on merge tools see :hg:`help merge-tools`.
     For configuring merge tools see the merge-tools_ section.
+``portablefilenames``
+    Check for portable filenames. Can be ``warn``, ``ignore`` or ``abort``.
+    Default is ``warn``.
+    If set to ``warn`` (or ``true``), a warning message is printed on POSIX
+    platforms, if a file with a non-portable filename is added (e.g. a file
+    with a name that can't be created on Windows because it contains reserved
+    parts like ``AUX`` or reserved characters like ``:``).
+    If set to ``ignore`` (or ``false``), no warning is printed.
+    If set to ``abort``, the command is aborted.
+    On Windows, this configuration option is ignored and the command aborted.
 ``quiet``
     Reduce the amount of output printed. True or False. Default is False.
 ``remotecmd``
diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -8,7 +8,7 @@
 from node import hex, nullid, nullrev, short
 from i18n import _
 import os, sys, errno, re, glob, tempfile
-import util, templater, patch, error, encoding, templatekw
+import util, scmutil, templater, patch, error, encoding, templatekw
 import match as matchmod
 import similar, revset, subrepo
 
@@ -435,7 +435,7 @@
         src = repo.wjoin(abssrc)
         state = repo.dirstate[abstarget]
 
-        util.checkfilename(abstarget)
+        scmutil.checkportable(ui, abstarget)
 
         # check for collisions
         prevsrc = targets.get(abstarget)
diff --git a/mercurial/context.py b/mercurial/context.py
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -7,7 +7,7 @@
 
 from node import nullid, nullrev, short, hex
 from i18n import _
-import ancestor, bdiff, error, util, subrepo, patch, encoding
+import ancestor, bdiff, error, util, scmutil, subrepo, patch, encoding
 import os, errno, stat
 
 propertycache = util.propertycache
@@ -801,6 +801,7 @@
         try:
             rejected = []
             for f in list:
+                scmutil.checkportable(ui, join(f))
                 p = self._repo.wjoin(f)
                 try:
                     st = os.lstat(p)
diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
new file mode 100644
--- /dev/null
+++ b/mercurial/scmutil.py
@@ -0,0 +1,27 @@
+# scmutil.py - Mercurial core utility functions
+#
+#  Copyright Matt Mackall <mpm at selenic.com>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+from i18n import _
+import util, error
+import os
+
+def checkportable(ui, f):
+    '''Check if filename f is portable and warn or abort depending on config'''
+    util.checkfilename(f)
+    val = ui.config('ui', 'portablefilenames', 'warn')
+    lval = val.lower()
+    abort = os.name == 'nt' or lval == 'abort'
+    bval = util.parsebool(val)
+    if abort or lval == 'warn' or bval:
+        msg = util.checkwinfilename(f)
+        if msg:
+            if abort:
+                raise util.Abort("%s: %r" % (msg, f))
+            ui.warn(_("warning: %s: %r\n") % (msg, f))
+    elif bval is None and lval != 'ignore':
+        raise error.ConfigError(
+            _("ui.portablefilenames value is invalid ('%s')") % val)
diff --git a/tests/test-add.t b/tests/test-add.t
--- a/tests/test-add.t
+++ b/tests/test-add.t
@@ -33,6 +33,41 @@
   A a
   A b
 
+  $ echo foo > con.xml
+  $ hg --config ui.portablefilenames=jump add con.xml
+  abort: ui.portablefilenames value is invalid ('jump')
+  [255]
+  $ hg --config ui.portablefilenames=abort add con.xml
+  abort: filename contains 'con', which is reserved on Windows: 'con.xml'
+  [255]
+  $ hg st
+  A a
+  A b
+  ? con.xml
+  $ hg add con.xml
+  warning: filename contains 'con', which is reserved on Windows: 'con.xml'
+  $ hg st
+  A a
+  A b
+  A con.xml
+  $ echo bla > 'hello:world'
+  $ hg --config ui.portablefilenames=abort add
+  adding hello:world
+  abort: filename contains ':', which is reserved on Windows: 'hello:world'
+  [255]
+  $ hg st
+  A a
+  A b
+  A con.xml
+  ? hello:world
+  $ hg --config ui.portablefilenames=ignore add
+  adding hello:world
+  $ hg st
+  A a
+  A b
+  A con.xml
+  A hello:world
+
   $ hg ci -m 0 --traceback
 
 should fail
diff --git a/tests/test-addremove.t b/tests/test-addremove.t
--- a/tests/test-addremove.t
+++ b/tests/test-addremove.t
@@ -10,14 +10,17 @@
   foo
   committed changeset 0:6f7f953567a2
   $ cd dir/
-  $ touch ../foo_2 bar_2
+  $ touch ../foo_2 bar_2 con.xml
   $ hg -v addremove
   adding dir/bar_2
+  adding dir/con.xml
   adding foo_2
+  warning: filename contains 'con', which is reserved on Windows: 'dir/con.xml'
   $ hg -v commit -m "add 2"
   dir/bar_2
+  dir/con.xml
   foo_2
-  committed changeset 1:e65414bf35c5
+  committed changeset 1:6bb597da00f1
 
   $ cd ..
   $ hg init sim
diff --git a/tests/test-copy.t b/tests/test-copy.t
--- a/tests/test-copy.t
+++ b/tests/test-copy.t
@@ -4,6 +4,9 @@
   $ hg commit -m "1"
   $ hg status
   $ hg copy a b
+  $ hg --config ui.portablefilenames=abort copy a con.xml
+  abort: filename contains 'con', which is reserved on Windows: 'con.xml'
+  [255]
   $ hg status
   A b
   $ hg sum
diff --git a/tests/test-hgweb-raw.t b/tests/test-hgweb-raw.t
--- a/tests/test-hgweb-raw.t
+++ b/tests/test-hgweb-raw.t
@@ -10,6 +10,7 @@
   > care about things like that.
   > ENDSOME
   $ hg add 'sub/some "text".txt'
+  warning: filename contains '"', which is reserved on Windows: 'sub/some "text".txt'
   $ hg commit -d "1 0" -m "Just some text"
 
   $ hg serve -p $HGPORT -A access.log -E error.log -d --pid-file=hg.pid
diff --git a/tests/test-rename.t b/tests/test-rename.t
--- a/tests/test-rename.t
+++ b/tests/test-rename.t
@@ -11,6 +11,9 @@
 rename a single file
 
   $ hg rename d1/d11/a1 d2/c
+  $ hg --config ui.portablefilenames=abort rename d1/a d1/con.xml
+  abort: filename contains 'con', which is reserved on Windows: 'd1/con.xml'
+  [255]
   $ hg sum
   parent: 0:9b4b6e7b2c26 tip
    1
diff --git a/tests/test-walk.t b/tests/test-walk.t
--- a/tests/test-walk.t
+++ b/tests/test-walk.t
@@ -29,6 +29,7 @@
   adding mammals/Procyonidae/coatimundi
   adding mammals/Procyonidae/raccoon
   adding mammals/skunk
+  warning: filename contains ':', which is reserved on Windows: 'glob:glob'
   $ hg commit -m "commit #0"
 
   $ hg debugwalk


More information about the Mercurial-devel mailing list