diff --git a/mercurial/match.py b/mercurial/match.py --- a/mercurial/match.py +++ b/mercurial/match.py @@ -1323,7 +1323,7 @@ def _globre(pat): return res -FLAG_RE = util.re.compile(b'^\(\?([aiLmsux]+)\)') +FLAG_RE = util.re.compile(b'^\(\?([aiLmsux]+)\)(.*)') def _regex(kind, pat, globsuffix): @@ -1354,11 +1354,15 @@ def _regex(kind, pat, globsuffix): return b'.*' + globre[len(b'[^/]*') :] + globsuffix return b'(?:|.*/)' + globre + globsuffix if kind == b'relre': - if pat.startswith(b'^'): - return pat - if FLAG_RE.match(pat): - return FLAG_RE.sub(br'(?\1:.*', pat) + b')' - return b'.*' + pat + flag = None + m = FLAG_RE.match(pat) + if m: + flag, pat = m.groups() + if not pat.startswith(b'^'): + pat = b'.*' + pat + if flag is not None: + pat = br'(?%s:%s)' % (flag, pat) + return pat if kind in (b'glob', b'rootglob'): return _globre(pat) + globsuffix raise error.ProgrammingError(b'not a regex pattern: %s:%s' % (kind, pat)) diff --git a/rust/hg-core/src/filepatterns.rs b/rust/hg-core/src/filepatterns.rs --- a/rust/hg-core/src/filepatterns.rs +++ b/rust/hg-core/src/filepatterns.rs @@ -205,7 +205,14 @@ fn _build_single_regex(entry: &IgnorePat &b"(?"[..], &pattern[s + 2..e - 1], &b":"[..], - &b".*"[..], + if pattern[e] == b'^' + || pattern[e] == b'*' + || pattern[e..].starts_with(b".*") + { + &b""[..] + } else { + &b".*"[..] + }, &pattern[e..], &b")"[..], ] @@ -752,5 +759,14 @@ mod tests { .unwrap(), Some(b"(?ia:.*ba{2}r)".to_vec()), ); + assert_eq!( + build_single_regex(&IgnorePattern::new( + PatternSyntax::RelRegexp, + b"(?ia)^ba{2}r", + Path::new("") + )) + .unwrap(), + Some(b"(?ia:^ba{2}r)".to_vec()), + ); } } diff --git a/tests/test-hgignore.t b/tests/test-hgignore.t --- a/tests/test-hgignore.t +++ b/tests/test-hgignore.t @@ -74,6 +74,8 @@ regexp with flag is the first one A dir/b.o ? a.c ? syntax + $ hg debugignore + regex with flag is not the first one @@ -83,6 +85,8 @@ regex with flag is not the first one A dir/b.o ? a.c ? syntax + $ hg debugignore + flag in a pattern should affect that pattern only @@ -93,6 +97,8 @@ flag in a pattern should affect that pat ? .hgignore ? a.c ? syntax + $ hg debugignore + $ echo 're:.HGIGNORE' > .hgignore $ echo 're:(?i)\.O$' >> .hgignore @@ -101,6 +107,32 @@ flag in a pattern should affect that pat ? .hgignore ? a.c ? syntax + $ hg debugignore + + +Check that '^' after flag is properly detected. + + $ echo 're:(?i)^[^a].*\.O$' > .hgignore + $ echo 're:.HGIGNORE' >> .hgignore + $ hg status + A dir/b.o + ? .hgignore + ? a.c + ? a.o + ? syntax + $ hg debugignore + + + $ echo 're:.HGIGNORE' > .hgignore + $ echo 're:(?i)^[^a].*\.O$' >> .hgignore + $ hg status + A dir/b.o + ? .hgignore + ? a.c + ? a.o + ? syntax + $ hg debugignore + further testing