[PATCH] Allow extensions to add new hgignore syntaxes
Boris Figovsky
borfig at gmail.com
Sat Jul 3 03:33:51 CDT 2010
Hello,
This patch allows extensions to add new syntaxes to .hgignore.
An example extension is attached.
# HG changeset patch
# User Boris Figovsky <borfig at gmail.com>
# Date 1278145261 -10800
# Node ID 68f65947ef90819e0f062b2d53e727351efa179e
# Parent 8b452fe4bf506a1a08bbc7e1bac81aceda8f4d10
Allow extensions to add new hgignore syntaxes
diff -r 8b452fe4bf50 -r 68f65947ef90 mercurial/ignore.py
--- a/mercurial/ignore.py Mon Jun 21 17:02:48 2010 -0300
+++ b/mercurial/ignore.py Sat Jul 03 11:21:01 2010 +0300
@@ -10,12 +10,13 @@
import re
_commentre = None
+_syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'}
def ignorepats(lines):
'''parse lines (iterable) of .hgignore text, returning a tuple of
(patterns, parse errors). These patterns should be given to compile()
to be validated and converted into a match function.'''
- syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'}
+ syntaxes = _syntaxes
syntax = 'relre:'
patterns = []
warnings = []
diff -r 8b452fe4bf50 -r 68f65947ef90 mercurial/match.py
--- a/mercurial/match.py Mon Jun 21 17:02:48 2010 -0300
+++ b/mercurial/match.py Sat Jul 03 11:21:01 2010 +0300
@@ -110,12 +110,14 @@
def patkind(pat):
return _patsplit(pat, None)[0]
+_kinds = ['re', 'glob', 'path', 'relglob', 'relpath', 'relre']
+
def _patsplit(pat, default):
"""Split a string into an optional pattern kind prefix and the
actual pattern."""
if ':' in pat:
kind, val = pat.split(':', 1)
- if kind in ('re', 'glob', 'path', 'relglob', 'relpath', 'relre'):
+ if kind in _kinds:
return kind, val
return default, pat
@@ -175,23 +177,21 @@
res += escape(c)
return res
+_regex_builders = {
+ 're' : lambda name, tail: name,
+ 'path' : lambda name, tail: '^' + re.escape(name) + '(?:/|$)',
+ 'glob' : lambda name, tail: _globre(name) + tail,
+ 'relglob' : lambda name, tail: '(?:|.*/)' + _globre(name) + tail,
+ 'relpath' : lambda name, tail: re.escape(name) + '(?:/|$)',
+ 'relre' : lambda name, tail: name if name.startswith('^') else '.*' + name,
+ }
+
def _regex(kind, name, tail):
'''convert a pattern into a regular expression'''
if not name:
return ''
- if kind == 're':
- return name
- elif kind == 'path':
- return '^' + re.escape(name) + '(?:/|$)'
- elif kind == 'relglob':
- return '(?:|.*/)' + _globre(name) + tail
- elif kind == 'relpath':
- return re.escape(name) + '(?:/|$)'
- elif kind == 'relre':
- if name.startswith('^'):
- return name
- return '.*' + name
- return _globre(name) + tail
+ regex_builder = _regex_builders[kind]
+ return regex_builder(name, tail)
def _buildmatch(pats, tail):
"""build a matching function from a set of patterns"""
# HG changeset patch
# User Boris Figovsky <borfig at gmail.com>
# Date 1278145409 -10800
# Node ID 0ec70448c582e60caace785dfef1f39850494482
# Parent 8b452fe4bf506a1a08bbc7e1bac81aceda8f4d10
Allow extensions to add new hgignore syntaxes
diff -r 8b452fe4bf50 -r 0ec70448c582 mercurial/ignore.py
--- a/mercurial/ignore.py Mon Jun 21 17:02:48 2010 -0300
+++ b/mercurial/ignore.py Sat Jul 03 11:23:29 2010 +0300
@@ -10,12 +10,13 @@
import re
_commentre = None
+_syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'}
def ignorepats(lines):
'''parse lines (iterable) of .hgignore text, returning a tuple of
(patterns, parse errors). These patterns should be given to compile()
to be validated and converted into a match function.'''
- syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'}
+ syntaxes = _syntaxes
syntax = 'relre:'
patterns = []
warnings = []
diff -r 8b452fe4bf50 -r 0ec70448c582 mercurial/match.py
--- a/mercurial/match.py Mon Jun 21 17:02:48 2010 -0300
+++ b/mercurial/match.py Sat Jul 03 11:23:29 2010 +0300
@@ -110,12 +110,14 @@
def patkind(pat):
return _patsplit(pat, None)[0]
+_kinds = ['re', 'glob', 'path', 'relglob', 'relpath', 'relre']
+
def _patsplit(pat, default):
"""Split a string into an optional pattern kind prefix and the
actual pattern."""
if ':' in pat:
kind, val = pat.split(':', 1)
- if kind in ('re', 'glob', 'path', 'relglob', 'relpath', 'relre'):
+ if kind in _kinds:
return kind, val
return default, pat
@@ -175,23 +177,21 @@
res += escape(c)
return res
+_regex_builders = {
+ 're' : lambda name, tail: name,
+ 'path' : lambda name, tail: '^' + re.escape(name) + '(?:/|$)',
+ 'glob' : lambda name, tail: _globre(name) + tail,
+ 'relglob' : lambda name, tail: '(?:|.*/)' + _globre(name) + tail,
+ 'relpath' : lambda name, tail: re.escape(name) + '(?:/|$)',
+ 'relre' : lambda name, tail: name if name.startswith('^') else '.*' + name,
+ }
+
def _regex(kind, name, tail):
'''convert a pattern into a regular expression'''
if not name:
return ''
- if kind == 're':
- return name
- elif kind == 'path':
- return '^' + re.escape(name) + '(?:/|$)'
- elif kind == 'relglob':
- return '(?:|.*/)' + _globre(name) + tail
- elif kind == 'relpath':
- return re.escape(name) + '(?:/|$)'
- elif kind == 'relre':
- if name.startswith('^'):
- return name
- return '.*' + name
- return _globre(name) + tail
+ regex_builder = _regex_builders[kind]
+ return regex_builder(name, tail)
def _buildmatch(pats, tail):
"""build a matching function from a set of patterns"""
-------------- next part --------------
A non-text attachment was scrubbed...
Name: twice.py
Type: text/x-python
Size: 205 bytes
Desc: not available
URL: <http://selenic.com/pipermail/mercurial-devel/attachments/20100703/135ef5a0/attachment.py>
More information about the Mercurial-devel
mailing list