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: improve git file discovery with untracked and removed files #1650

Merged
merged 4 commits into from Jul 10, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/ansiblelint/file_utils.py
Expand Up @@ -206,14 +206,31 @@ def __repr__(self) -> str:
def discover_lintables(options: Namespace) -> Dict[str, Any]:
"""Find all files that we know how to lint."""
# git is preferred as it also considers .gitignore
git_command = ['git', 'ls-files', '-z']
git_command_present = [
'git',
'ls-files',
'--cached',
'--others',
'--exclude-standard',
'-z',
]
git_command_absent = ['git', 'ls-files', '--deleted', '-z']
out = None

try:
out = subprocess.check_output(
git_command, stderr=subprocess.STDOUT, universal_newlines=True
out_present = subprocess.check_output(
git_command_present, stderr=subprocess.STDOUT, universal_newlines=True
).split("\x00")[:-1]
_logger.info("Discovered files to lint using: %s", ' '.join(git_command))
_logger.info(
"Discovered files to lint using: %s", ' '.join(git_command_present)
)

out_absent = subprocess.check_output(
git_command_absent, stderr=subprocess.STDOUT, universal_newlines=True
).split("\x00")[:-1]
_logger.info("Excluded removed files using: %s", ' '.join(git_command_absent))

out = set(out_present) - set(out_absent)
except subprocess.CalledProcessError as exc:
if not (exc.returncode == 128 and 'fatal: not a git repository' in exc.output):
_logger.warning(
Expand Down
6 changes: 5 additions & 1 deletion test/TestUtils.py
Expand Up @@ -354,7 +354,11 @@ def test_cli_auto_detect(capfd: CaptureFixture[str]) -> None:
out, err = capfd.readouterr()

# Confirmation that it runs in auto-detect mode
assert "Discovered files to lint using: git ls-files -z" in err
assert (
"Discovered files to lint using: git ls-files --cached --others --exclude-standard -z"
in err
)
assert "Excluded removed files using: git ls-files --deleted -z" in err
# An expected rule match from our examples
assert (
"examples/playbooks/empty_playbook.yml:1: "
Expand Down