Skip to content

! doesn't exclude files in directories if the pattern doesn't have a trailing slash #57

@Jackenmen

Description

@Jackenmen

It appears that pathspec is not handling exclusions correctly for directories without a trailing slash:

import pathspec


gitignore = """\
build
!/foo/build
"""
spec = pathspec.PathSpec.from_lines("gitwildmatch", gitignore.splitlines())
# incorrectly returns True
print(spec.match_file("foo/build/file.py"))


gitignore = """\
build
!/foo/build/
"""
spec = pathspec.PathSpec.from_lines("gitwildmatch", gitignore.splitlines())
# correctly returns False
print(spec.match_file("foo/build/file.py"))

If you try doing the same with .gitignore, it works correctly:

git init repro
cd repro
echo $'build\n!/foo/build' > .gitignore
mkdir build
touch build/file.py
mkdir -p foo/build
touch foo/build/file.py

Running git status gives:

On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.gitignore
	foo/

nothing added to commit but untracked files present (use "git add" to track)

And running git ls-files --others --exclude-standard gives:

.gitignore
foo/build/file.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions