Skip to content

Commit

Permalink
Add ability to ignore patterns directly from cli (#4)
Browse files Browse the repository at this point in the history
* Add ability to ignore patterns directly from cli
* Updated for latest changes to main

---------

Co-authored-by: Simon Willison <swillison@gmail.com>
  • Loading branch information
dipam7 and simonw authored Apr 8, 2024
1 parent ebd191f commit f8af0fa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
23 changes: 20 additions & 3 deletions files_to_prompt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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
)
25 changes: 25 additions & 0 deletions tests/test_files_to_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit f8af0fa

Please sign in to comment.