Skip to content

Commit 14116aa

Browse files
committed
feat: exclude files from source tree
closes feature request #14.
1 parent 96b3a8e commit 14116aa

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ Exclude files using glob patterns:
9797
code2prompt path/to/codebase --exclude="*.txt,*.md"
9898
```
9999

100+
Exclude files/folders from the source tree based on exclude patterns:
101+
102+
```sh
103+
code2prompt path/to/codebase --exclude="*.npy,*.wav" --exclude-from-tree
104+
```
105+
100106
Display the token count of the generated prompt:
101107

102108
```sh

src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ struct Cli {
3939
#[clap(long)]
4040
include_priority: bool,
4141

42+
/// Exclude files/folders from the source tree based on exclude patterns
43+
#[clap(long)]
44+
exclude_from_tree: bool,
45+
4246
/// Display the token count of the generated prompt
4347
#[clap(long)]
4448
tokens: bool,
@@ -97,6 +101,7 @@ fn main() -> Result<()> {
97101
args.include_priority,
98102
args.line_number,
99103
args.relative_paths,
104+
args.exclude_from_tree,
100105
);
101106

102107
let (tree, files) = match create_tree {

src/path.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub fn traverse_directory(
3030
include_priority: bool,
3131
line_number: bool,
3232
relative_paths: bool,
33+
exclude_from_tree: bool,
3334
) -> Result<(String, Vec<serde_json::Value>)> {
3435
// ~~~ Initialization ~~~
3536
let mut files = Vec::new();
@@ -47,6 +48,12 @@ pub fn traverse_directory(
4748
let mut current_tree = &mut root;
4849
for component in relative_path.components() {
4950
let component_str = component.as_os_str().to_string_lossy().to_string();
51+
52+
// Check if the current component should be excluded from the tree
53+
if exclude_from_tree && !should_include_file(path, include, exclude, include_priority) {
54+
break;
55+
}
56+
5057
current_tree = if let Some(pos) = current_tree
5158
.leaves
5259
.iter_mut()

0 commit comments

Comments
 (0)