[PATCH 7 of 8] ignore: add support for including subdir .hgignores
Durham Goode
durham at fb.com
Thu May 14 15:57:33 CDT 2015
On 5/13/15, 8:03 PM, "Siddharth Agarwal" <sid at less-broken.com> wrote:
>On 05/13/2015 08:13 AM, Durham Goode wrote:
>> # HG changeset patch
>> # User Durham Goode <durham at fb.com>
>> # Date 1431485142 25200
>> # Tue May 12 19:45:42 2015 -0700
>> # Node ID 42bafc92b436181ba5d6568967577a214198648d
>> # Parent f058e14998c947d86ccbdd17edaf2392b4190cce
>> ignore: add support for including subdir .hgignores
>
>This should have a (BC) note.
>
>> This adds support for including sub-hgignore files in a .hgignore. The
>>syntax is
>> '%include path/to/.hgignore' and is relative to the current .hgignore.
>
>I actually suspect a better syntax might '#include path/to/.hgignore',
>so that older Mercurial doesn't try and interpret the ignore rules as a
>pattern. I know this makes it different from config rules, though.
Talked a bit in IRC about this. Moving to a
'subinclude:path/to/.hgignore' style syntax might be nice, since then
users could use a similar pattern of 'include:path/to/.hgignore' with -I
and -X and stuff. I'll see if I can make that happen, then resend.
>
>> The rules
>> inside the sub-ignore file only apply to files in that subdirectory.
>>
>> At the moment we only support globs in sub-ignore files. regexs will
>>cause an
>> exception. This is because we can't reliabily
>(sp)
>> modify a regex to have a prefix
>> (ex: adding a prefix to '^foo|^bar' would require parsing the regex).
>>In a
>> subsequent patch we will add a config option to allow users to opt-in
>>to regexs
>> in sub-ignores if they're certain their regexs are sane.
>>
>> diff --git a/mercurial/ignore.py b/mercurial/ignore.py
>> --- a/mercurial/ignore.py
>> +++ b/mercurial/ignore.py
>> @@ -19,6 +19,13 @@ def ignorepats(ui, root, filepath):
>> syntax = 'relre:'
>> patterns = []
>> warnings = []
>> + subpaths = set()
>> +
>> + common = os.path.commonprefix([filepath, root])
>> + subdir = None
>> + if root and common == root:
>> + # Find the prefix that should be applied to these patterns
>> + subdir = os.path.dirname(filepath[len(root) + 1:])
>>
>> fp = open(filepath)
>>
>> @@ -35,6 +42,18 @@ def ignorepats(ui, root, filepath):
>> if not line:
>> continue
>>
>> + if line.startswith('%include '):
>> + subpath = line[9:]
>> + fullpath = os.path.join(os.path.dirname(filepath), subpath)
>> + fullpath = os.path.normpath(fullpath)
>> + subpats, subsubpaths = readignorefile(ui, root, fullpath)
>
>'subpats' is close enough to 'subpath' that my eyes crossed over while
>reading this. Could we change one of the names?
>
>> +
>> + subpaths.add(fullpath)
>> + subpaths.update(subsubpaths)
>> +
>> + patterns.extend(subpats)
>> + continue
>> +
>> if line.startswith('syntax:'):
>> s = line[7:].strip()
>> try:
>> @@ -53,22 +72,33 @@ def ignorepats(ui, root, filepath):
>> linesyntax = rels
>> line = line[len(s) + 1:]
>> break
>> +
>> + if subdir:
>> + # The ignore file is inside the repo, so make its patterns
>> + # relative to its location.
>> + if linesyntax == 'relre:':
>> + message = _('cannot use sub-ignore files that use
>>regular '
>> + 'expressions')
>> + raise util.Abort(message)
>> + elif linesyntax == 'relglob:':
>> + line = '%s/*%s' % (subdir, line)
>
>What happens if linesyntax is something else?
I don't think ignore files ever worked with anything besides glob and
regex. But if it's something else, we'll just not prefix it.
More information about the Mercurial-devel
mailing list