filesets: add eol predicate
authorMatt Mackall <mpm@selenic.com>
Fri, 29 Mar 2013 16:48:32 -0700
changeset 18842 3ce3f2b059a1
parent 18841 e978fb3038e0
child 18844 37f8baaf31d2
filesets: add eol predicate
mercurial/fileset.py
tests/test-fileset.t
--- a/mercurial/fileset.py	Fri Mar 29 15:27:33 2013 -0700
+++ b/mercurial/fileset.py	Fri Mar 29 16:48:32 2013 -0700
@@ -353,6 +353,29 @@
 
     return s
 
+def eol(mctx, x):
+    """``eol(style)``
+    File contains newlines of the given style (dos, unix, mac). Binary
+    files are excluded, files with mixed line endings match multiple
+    styles.
+    """
+
+    # i18n: "encoding" is a keyword
+    enc = getstring(x, _("encoding requires an encoding name"))
+
+    s = []
+    for f in mctx.existing():
+        d = mctx.ctx[f].data()
+        if util.binary(d):
+            continue
+        if (enc == 'dos' or enc == 'win') and '\r\n' in d:
+            s.append(f)
+        elif enc == 'unix' and re.search('(?<!\r)\n', d):
+            s.append(f)
+        elif enc == 'mac' and re.search('\r(?!\n)', d):
+            s.append(f)
+    return s
+
 def copied(mctx, x):
     """``copied()``
     File that is recorded as being copied.
@@ -395,6 +418,7 @@
     'copied': copied,
     'deleted': deleted,
     'encoding': encoding,
+    'eol': eol,
     'exec': exec_,
     'grep': grep,
     'ignored': ignored,
--- a/tests/test-fileset.t	Fri Mar 29 15:27:33 2013 -0700
+++ b/tests/test-fileset.t	Fri Mar 29 16:48:32 2013 -0700
@@ -226,3 +226,21 @@
   b2
   c1
 
+  >>> open('dos', 'wb').write("dos\r\n")
+  >>> open('mixed', 'wb').write("dos\r\nunix\n")
+  >>> open('mac', 'wb').write("mac\r")
+  $ hg add dos mixed mac
+
+  $ fileset 'eol(dos)'
+  dos
+  mixed
+  $ fileset 'eol(unix)'
+  .hgsub
+  .hgsubstate
+  a1
+  b1
+  b2
+  c1
+  mixed
+  $ fileset 'eol(mac)'
+  mac