Skip to content

Commit

Permalink
Merge pull request #1078 from carmenbianca/fix-asterisk-slash
Browse files Browse the repository at this point in the history
Fix globbing
  • Loading branch information
carmenbianca authored Sep 26, 2024
2 parents 3deeff9 + 0f5a87d commit aea0d07
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
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

0 comments on commit aea0d07

Please sign in to comment.