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

Avoid warnings when not running inside git repos #1475

Merged
merged 1 commit into from Mar 18, 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
28 changes: 19 additions & 9 deletions src/ansiblelint/file_utils.py
Expand Up @@ -212,10 +212,11 @@ def get_yaml_files(options: Namespace) -> Dict[str, Any]:
git_command, stderr=subprocess.STDOUT, universal_newlines=True
).split("\x00")[:-1]
except subprocess.CalledProcessError as exc:
_logger.warning(
"Failed to discover yaml files to lint using git: %s",
exc.output.rstrip('\n'),
)
if not (exc.returncode == 128 and 'fatal: not a git repository' in exc.output):
_logger.warning(
"Failed to discover yaml files to lint using git: %s",
exc.output.rstrip('\n'),
)
except FileNotFoundError as exc:
if options.verbosity:
_logger.warning("Failed to locate command: %s", exc)
Expand Down Expand Up @@ -253,10 +254,19 @@ def guess_project_dir() -> str:

def expand_dirs_in_lintables(lintables: Set[Lintable]) -> None:
"""Return all recognized lintables within given directory."""
all_files = get_yaml_files(options)
should_expand = False

for item in copy.copy(lintables):
for item in lintables:
if item.path.is_dir():
for filename in all_files:
if filename.startswith(str(item.path)):
lintables.add(Lintable(filename))
should_expand = True
break

if should_expand:
# this relies on git and we do not want to call unless needed
all_files = get_yaml_files(options)

for item in copy.copy(lintables):
if item.path.is_dir():
for filename in all_files:
if filename.startswith(str(item.path)):
lintables.add(Lintable(filename))
4 changes: 2 additions & 2 deletions test/TestUtils.py
Expand Up @@ -213,9 +213,9 @@ def test_expand_paths_vars(test_path, expected, monkeypatch):
('reset_env_var', 'message_prefix'),
(
('PATH', "Failed to locate command: "),
('GIT_DIR', "Failed to discover yaml files to lint using git: "),
('GIT_DIR', "Discovering files to lint: "),
),
ids=('no Git installed', 'outside Git repository'),
ids=('no-git-cli', 'outside-git-repo'),
)
def test_get_yaml_files_git_verbose(reset_env_var, message_prefix, monkeypatch, caplog):
"""Ensure that autodiscovery lookup failures are logged."""
Expand Down