Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix globbing #1078

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.d/fixed/glob.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Fixed the globbing of a single asterisk succeeded by a slash (e.g.
`directory-*/foo.py`). The glob previously did nothing. (#1078)
5 changes: 4 additions & 1 deletion src/reuse/global_licensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ class AnnotationsItem:

def __attrs_post_init__(self) -> None:
def translate(path: str) -> str:
# pylint: disable=too-many-branches
blocks = []
escaping = False
globstar = False
Expand All @@ -360,7 +361,7 @@ def translate(path: str) -> str:
if char == "\\":
if prev_char == "\\" and escaping:
escaping = False
blocks.append(r"\\")
blocks.append("\\\\")
else:
escaping = True
elif char == "*":
Expand All @@ -372,6 +373,8 @@ def translate(path: str) -> str:
blocks.append(r".*")
elif char == "/":
if not globstar:
if prev_char == "*":
blocks.append("[^/]*")
blocks.append("/")
escaping = False
else:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_global_licensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ def test_asterisk_asterisk(self):
assert item.matches("src/foo.py")
assert item.matches(".foo/bar")

def test_asterisk_slash(self):
"""Handle asterisk slash."""
item = AnnotationsItem(paths=[r"*/file"])
assert item.matches("foo/file")

def test_escape_asterisk(self):
"""Handle escape asterisk."""
item = AnnotationsItem(paths=[r"\*.py"])
Expand Down Expand Up @@ -293,6 +298,7 @@ def test_escape_escape_asterisk(self):
"""Handle escape escape asterisk."""
item = AnnotationsItem(paths=[r"\\*.py"])
assert item.matches(r"\foo.py")
assert not item.matches(r"foo.py")

def test_asterisk_asterisk_asterisk(self):
"""Handle asterisk asterisk asterisk."""
Expand Down
Loading