Skip to content

Commit 9907b68

Browse files
authored
chore: replace pathlib.PurePosixPath.full_match with a translated regex (#856)
This allows to use both `/` and `\\` in the patterns to match any filename that could also have either of them, independently of the OS on which the code is running.
1 parent 2088431 commit 9907b68

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

mergify_cli/ci/scopes/cli.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from __future__ import annotations
22

3+
import glob
34
import json
45
import os
56
import pathlib
7+
import re
68
import typing
79
import uuid
810

@@ -21,29 +23,45 @@
2123
GITHUB_ACTIONS_SCOPES_OUTPUT_NAME = "scopes"
2224

2325

26+
# NOTE: We convert the pattern to a compiled regex using `glob.translate`,
27+
# in order to avoid any potential inconsistency that could arise from
28+
# running `glob.glob` or pathlib.PurePath.full_match` on a different OS.
29+
def convert_pattern_to_regex(pattern: str) -> re.Pattern[str]:
30+
return re.compile(
31+
glob.translate(
32+
pattern,
33+
recursive=True,
34+
include_hidden=True,
35+
seps=["/", "\\"],
36+
),
37+
)
38+
39+
2440
def match_scopes(
2541
files: abc.Iterable[str],
2642
filters: dict[config.ScopeName, config.FileFilters],
2743
) -> tuple[set[str], dict[str, list[str]]]:
2844
scopes_hit: set[str] = set()
2945
per_scope: dict[str, list[str]] = {s: [] for s in filters}
3046
for f in files:
31-
# NOTE(sileht): we use pathlib.full_match to support **, as fnmatch does not
32-
p = pathlib.PurePosixPath(f)
3347
for scope, scope_config in filters.items():
3448
if not scope_config.include and not scope_config.exclude:
3549
continue
3650

3751
# Check if file matches any include
3852
if scope_config.include:
3953
matches_positive = any(
40-
p.full_match(pat) for pat in scope_config.include
54+
convert_pattern_to_regex(pat).fullmatch(f)
55+
for pat in scope_config.include
4156
)
4257
else:
4358
matches_positive = True
4459

4560
# Check if file matches any exclude
46-
matches_negative = any(p.full_match(pat) for pat in scope_config.exclude)
61+
matches_negative = any(
62+
convert_pattern_to_regex(pat).fullmatch(f)
63+
for pat in scope_config.exclude
64+
)
4765

4866
# File matches the scope if it matches positive patterns and doesn't match negative patterns
4967
if matches_positive and not matches_negative:

0 commit comments

Comments
 (0)