Skip to content

Commit

Permalink
fix: improve git file discovery with untracked and removed files (#1650)
Browse files Browse the repository at this point in the history
* fix: improve git file discovery with untracked and removed files (#1647)

Signed-off-by: Xabier Napal <xabiernapal@pm.me>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* test: update git ls-files assertions (#1647)

Signed-off-by: Xabier Napal <xabiernapal@pm.me>

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sorin Sbarnea <ssbarnea@redhat.com>
  • Loading branch information
3 people committed Jul 10, 2021
1 parent 32a6286 commit 82766e2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
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

0 comments on commit 82766e2

Please sign in to comment.