changeset 14684:87b9d6a7d807

fileset: add encoding() predicate
author Matt Mackall <mpm@selenic.com>
date Sat, 18 Jun 2011 16:53:49 -0500
parents 281102f37b24
children 394121d9f4fc
files mercurial/fileset.py
diffstat 1 files changed, 23 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/fileset.py	Sat Jun 18 16:53:49 2011 -0500
+++ b/mercurial/fileset.py	Sat Jun 18 16:53:49 2011 -0500
@@ -313,11 +313,34 @@
 
     return [f for f in mctx.subset if m(mctx.ctx[f].size())]
 
+def encoding(mctx, x):
+    """``encoding(name)``
+    File can be successfully decoded with the given character
+    encoding. May not be useful for encodings other than ASCII and
+    UTF-8.
+    """
+
+    enc = getstring(x, _("encoding requires an encoding name"))
+
+    s = []
+    for f in mctx.subset:
+        d = mctx.ctx[f].data()
+        try:
+            d.decode(enc)
+        except LookupError:
+            raise util.Abort(_("unknown encoding '%s'") % enc)
+        except UnicodeDecodeError:
+            continue
+        s.append(f)
+
+    return s
+
 symbols = {
     'added': added,
     'binary': binary,
     'clean': clean,
     'deleted': deleted,
+    'encoding': encoding,
     'exec': exec_,
     'grep': grep,
     'ignored': ignored,