diff --git a/nf_core/utils.py b/nf_core/utils.py index 86de5a5fd1..211d86ad55 100644 --- a/nf_core/utils.py +++ b/nf_core/utils.py @@ -6,7 +6,6 @@ import concurrent.futures import datetime import errno -import fnmatch import hashlib import io import json @@ -1671,27 +1670,12 @@ def set_wd(path: Path) -> Generator[None, None, None]: def get_wf_files(wf_path: Path): - """Return a list of all files in a directory (ignores .gitigore files)""" - - wf_files = [] - - ignore = [".git/*"] - try: - with open(Path(wf_path, ".gitignore")) as f: - for line in f.read().splitlines(): - if not line or line.startswith("#"): - continue - # Make trailing-slash patterns match their entire subtree - line = re.sub("/$", "/*", line) - ignore.append(line) - except FileNotFoundError: - pass - - for path in Path(wf_path).rglob("*"): - rpath = str(path.relative_to(wf_path)) - if any(fnmatch.fnmatch(rpath, pattern) for pattern in ignore): - continue - if path.is_file(): - wf_files.append(str(path)) - - return wf_files + """Return a list of all files in a directory (respects .gitignore via git ls-files)""" + git_ls_files = subprocess.check_output( + ["git", "ls-files"], + cwd=wf_path, + stderr=subprocess.DEVNULL, + ).splitlines() + return [ + str(Path(wf_path) / fn.decode("utf-8")) for fn in git_ls_files if (Path(wf_path) / fn.decode("utf-8")).is_file() + ] diff --git a/tests/pipelines/lint/test_if_empty_null.py b/tests/pipelines/lint/test_if_empty_null.py index a813a06569..a998a152ae 100644 --- a/tests/pipelines/lint/test_if_empty_null.py +++ b/tests/pipelines/lint/test_if_empty_null.py @@ -1,3 +1,4 @@ +import subprocess from pathlib import Path import yaml @@ -33,6 +34,7 @@ def test_if_empty_null_throws_warn(self): "| ifEmpty(null)\n", ] ) + subprocess.check_call(["git", "add", "docs/test.txt"], cwd=self.new_pipeline) lint_obj = nf_core.pipelines.lint.PipelineLint(self.new_pipeline) lint_obj._load() result = lint_obj.pipeline_if_empty_null() diff --git a/tests/test_utils.py b/tests/test_utils.py index f72de515c1..2c524e4273 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,6 +1,7 @@ """Tests covering for utility functions.""" import os +import subprocess from pathlib import Path from unittest import mock @@ -228,9 +229,9 @@ def test_fetch_wf_config(self, mock_run_cmd): @with_temporary_folder def test_get_wf_files(self, tmpdir): tmpdir = Path(tmpdir) + subprocess.check_call(["git", "init"], cwd=tmpdir) (tmpdir / ".gitignore").write_text(".nextflow*\nwork/\nresults/\n") for rpath in [ - ".git/should-ignore-1", "work/should-ignore-2", "results/should-ignore-3", ".nextflow.should-ignore-4", @@ -240,6 +241,10 @@ def test_get_wf_files(self, tmpdir): p = tmpdir / rpath p.parent.mkdir(exist_ok=True) p.touch() + subprocess.check_call( + ["git", "add", ".gitignore", "dir1/should-match-1", "should-match-2"], + cwd=tmpdir, + ) files = nf_core.utils.get_wf_files(tmpdir) files = sorted(str(Path(f).relative_to(tmpdir)) for f in files) assert files == [".gitignore", "dir1/should-match-1", "should-match-2"]