Skip to content

Commit

Permalink
Move re flags at start of the expression.
Browse files Browse the repository at this point in the history
Closes #30.
  • Loading branch information
JulienPalard committed Feb 22, 2023
1 parent c144937 commit 6f48ff4
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions gitignore_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ def rule_from_pattern(pattern, base_path=None, source=None):
if striptrailingspaces:
pattern = pattern[:i]
i = i - 1
regex = fnmatch_pathname_to_regex(pattern, directory_only, negation)
if anchored:
regex = ''.join(['^', regex])
regex = fnmatch_pathname_to_regex(
pattern, directory_only, negation, anchored=bool(anchored)
)
return IgnoreRule(
pattern=orig_pattern,
regex=regex,
Expand Down Expand Up @@ -152,13 +152,15 @@ def match(self, abs_path: Union[str, Path]):

# Frustratingly, python's fnmatch doesn't provide the FNM_PATHNAME
# option that .gitignore's behavior depends on.
def fnmatch_pathname_to_regex(pattern, directory_only: bool, negation: bool):
def fnmatch_pathname_to_regex(
pattern, directory_only: bool, negation: bool, anchored: bool = False
):
"""
Implements fnmatch style-behavior, as though with FNM_PATHNAME flagged;
the path separator will not match shell-style '*' and '.' wildcards.
"""
i, n = 0, len(pattern)

seps = [re.escape(os.sep)]
if os.altsep is not None:
seps.append(re.escape(os.altsep))
Expand Down Expand Up @@ -205,6 +207,8 @@ def fnmatch_pathname_to_regex(pattern, directory_only: bool, negation: bool):
res.append('[{}]'.format(stuff))
else:
res.append(re.escape(c))
if anchored:
res.insert(0, '^')
res.insert(0, '(?ms)')
if not directory_only:
res.append('$')
Expand Down

0 comments on commit 6f48ff4

Please sign in to comment.