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

check root .gitignore in '_ignored' #3284

Merged
merged 6 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 18 additions & 10 deletions dvc/scm/git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from dvc.utils import relpath
from dvc.utils.fs import path_isin


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -133,15 +132,24 @@ def _get_gitignore(self, path):

return entry, gitignore

@staticmethod
def _ignored(entry, gitignore_path):
if os.path.exists(gitignore_path):
with open(gitignore_path, "r") as fobj:
ignore_list = fobj.readlines()
return any(
filter(lambda x: x.strip() == entry.strip(), ignore_list)
)
return False
def _ignored(self, entry, gitignore_path):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simply pass path to it now, since we no longer work with entries. πŸ™‚ Your PR is helping tidy this a lot πŸ™‚


# We want to check first if `entry` is already being ignored
# by the .gitignore file in the root dir.

entry = (
entry[1:] if entry[0] == "/" else entry
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
entry[1:] if entry[0] == "/" else entry
entry = entry.lstrip("/")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it won't be needed if we just pass path to _ignored() instead of putting the pieces back together.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm don't know what happened there haha

) # make sure that joining the paths works
entry_path = os.path.join(os.path.dirname(gitignore_path), entry)
from git.exc import GitCommandError

try:
self.repo.git.check_ignore(entry_path)
return True
except GitCommandError:
# If none of the paths passed to `check_ignore` are ignored,
# GitPython annoyingly raises an Exception
return False

def ignore(self, path):
entry, gitignore = self._get_gitignore(path)
Expand Down
8 changes: 8 additions & 0 deletions tests/func/test_scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ def test_ignore(tmp_dir, scm):
assert _count_gitignore_entries(target) == 0


def test_ignored(tmp_dir, scm):
tmp_dir.gen({"dir1": {"file1.jpg": "cont", "file2.txt": "cont"}})
tmp_dir.gen({".gitignore": "dir1/*.jpg"})

assert scm._ignored("file1.jpg", fspath(tmp_dir / "dir1" / ".gitignore"))
assert not scm._ignored("file2.txt", fspath(tmp_dir / "dir1" / ".gitignore"))


def test_get_gitignore(tmp_dir, scm):
tmp_dir.gen({"file1": "contents", "dir": {}})

Expand Down