Add ability to ignore patterns directly from cli (#4)

* Add ability to ignore patterns directly from cli
* Updated for latest changes to main

---------

Co-authored-by: Simon Willison <swillison@gmail.com>
This commit is contained in:
Dipam Vasani
2024-04-07 22:27:24 -07:00
committed by GitHub
parent ebd191f77d
commit f8af0fad7f
2 changed files with 45 additions and 3 deletions

View File

@@ -23,7 +23,9 @@ def read_gitignore(path):
return []
def process_path(path, include_hidden, ignore_gitignore, gitignore_rules):
def process_path(
path, include_hidden, ignore_gitignore, gitignore_rules, ignore_patterns
):
if os.path.isfile(path):
with open(path, "r") as f:
file_contents = f.read()
@@ -51,6 +53,13 @@ def process_path(path, include_hidden, ignore_gitignore, gitignore_rules):
if not should_ignore(os.path.join(root, f), gitignore_rules)
]
if ignore_patterns:
files = [
f
for f in files
if not any(fnmatch(f, pattern) for pattern in ignore_patterns)
]
for file in files:
file_path = os.path.join(root, file)
with open(file_path, "r") as f:
@@ -75,8 +84,14 @@ def process_path(path, include_hidden, ignore_gitignore, gitignore_rules):
is_flag=True,
help="Ignore .gitignore files and include all files",
)
@click.option(
"--ignore-patterns",
multiple=True,
default=[],
help="List of patterns to ignore",
)
@click.version_option()
def cli(paths, include_hidden, ignore_gitignore):
def cli(paths, include_hidden, ignore_gitignore, ignore_patterns):
"""
Takes one or more paths to files or directories and outputs every file,
recursively, each one preceded with its filename like this:
@@ -96,4 +111,6 @@ def cli(paths, include_hidden, ignore_gitignore):
raise click.BadArgumentUsage(f"Path does not exist: {path}")
if not ignore_gitignore:
gitignore_rules.extend(read_gitignore(os.path.dirname(path)))
process_path(path, include_hidden, ignore_gitignore, gitignore_rules)
process_path(
path, include_hidden, ignore_gitignore, gitignore_rules, ignore_patterns
)

View File

@@ -82,6 +82,31 @@ def test_multiple_paths(tmpdir):
assert "Contents of single file" in result.output
def test_ignore_patterns(tmpdir):
runner = CliRunner()
with tmpdir.as_cwd():
os.makedirs("test_dir")
with open("test_dir/file_to_ignore.txt", "w") as f:
f.write("This file should be ignored due to ignore patterns")
with open("test_dir/file_to_include.txt", "w") as f:
f.write("This file should be included")
result = runner.invoke(cli, ["test_dir", "--ignore-patterns", "*.txt"])
assert result.exit_code == 0
assert "test_dir/file_to_ignore.txt" not in result.output
assert "This file should be ignored due to ignore patterns" not in result.output
assert "test_dir/file_to_include.txt" not in result.output
result = runner.invoke(
cli, ["test_dir", "--ignore-patterns", "file_to_ignore.*"]
)
assert result.exit_code == 0
assert "test_dir/file_to_ignore.txt" not in result.output
assert "This file should be ignored due to ignore patterns" not in result.output
assert "test_dir/file_to_include.txt" in result.output
assert "This file should be included" in result.output
def test_mixed_paths_with_options(tmpdir):
runner = CliRunner()
with tmpdir.as_cwd():