vtcode_commons/
vtcodegitignore.rs1use anyhow::{Result, anyhow};
7use ignore::gitignore::{Gitignore, GitignoreBuilder};
8use std::path::{Path, PathBuf};
9use std::sync::Arc;
10use tokio::fs;
11
12#[derive(Debug, Clone)]
14pub struct VTCodeGitignore {
15 root_dir: PathBuf,
17 matcher: Gitignore,
19 loaded: bool,
21}
22
23impl VTCodeGitignore {
24 pub async fn new() -> Result<Self> {
26 let current_dir =
27 std::env::current_dir().map_err(|e| anyhow!("Failed to get current directory: {e}"))?;
28
29 Self::from_directory(¤t_dir).await
30 }
31
32 pub async fn from_directory(root_dir: &Path) -> Result<Self> {
34 let gitignore_path = root_dir.join(".vtcodegitignore");
35
36 let mut loaded = false;
37 let mut builder = GitignoreBuilder::new(root_dir);
38
39 if gitignore_path.exists() {
40 match Self::load_patterns(&gitignore_path, &mut builder).await {
41 Ok(()) => {
42 loaded = true;
43 }
44 Err(e) => {
45 tracing::warn!("Failed to load .vtcodegitignore: {}", e);
47 }
48 }
49 }
50
51 let matcher = builder.build().unwrap_or_else(|_| {
52 Gitignore::empty()
54 });
55
56 Ok(Self {
57 root_dir: root_dir.to_path_buf(),
58 matcher,
59 loaded,
60 })
61 }
62
63 async fn load_patterns(file_path: &Path, builder: &mut GitignoreBuilder) -> Result<()> {
65 let content = fs::read_to_string(file_path)
66 .await
67 .map_err(|e| anyhow!("Failed to read .vtcodegitignore: {e}"))?;
68
69 for (line_num, line) in content.lines().enumerate() {
70 let line = line.trim();
71
72 if line.is_empty() || line.starts_with('#') {
74 continue;
75 }
76
77 builder.add_line(None, line).map_err(|e| {
78 anyhow!(
79 "Invalid pattern on line {}: '{}': {}",
80 line_num + 1,
81 line,
82 e
83 )
84 })?;
85 }
86
87 Ok(())
88 }
89
90 pub fn should_exclude(&self, file_path: &Path) -> bool {
92 if !self.loaded {
93 return false;
94 }
95
96 let relative_path = match file_path.strip_prefix(&self.root_dir) {
98 Ok(rel) => rel,
99 Err(_) => file_path,
100 };
101
102 self.matcher
103 .matched_path_or_any_parents(relative_path, file_path.is_dir())
104 .is_ignore()
105 }
106
107 pub fn filter_paths(&self, paths: Vec<PathBuf>) -> Vec<PathBuf> {
109 if !self.loaded {
110 return paths;
111 }
112
113 paths
114 .into_iter()
115 .filter(|path| !self.should_exclude(path))
116 .collect()
117 }
118
119 pub fn is_loaded(&self) -> bool {
121 self.loaded
122 }
123
124 pub fn pattern_count(&self) -> usize {
126 self.matcher.num_ignores() as usize
127 }
128
129 pub fn root_dir(&self) -> &Path {
131 &self.root_dir
132 }
133}
134
135impl Default for VTCodeGitignore {
136 fn default() -> Self {
137 let root_dir = PathBuf::new();
138 let matcher = Gitignore::empty();
139 Self {
140 root_dir,
141 matcher,
142 loaded: false,
143 }
144 }
145}
146
147pub static VTCODE_GITIGNORE: once_cell::sync::Lazy<tokio::sync::RwLock<Arc<VTCodeGitignore>>> =
149 once_cell::sync::Lazy::new(|| tokio::sync::RwLock::new(Arc::new(VTCodeGitignore::default())));
150
151pub async fn initialize_vtcode_gitignore() -> Result<()> {
153 let gitignore = VTCodeGitignore::new().await?;
154 let mut global_gitignore = VTCODE_GITIGNORE.write().await;
155 *global_gitignore = Arc::new(gitignore);
156 Ok(())
157}
158
159pub async fn snapshot_global_vtcode_gitignore() -> Arc<VTCodeGitignore> {
161 VTCODE_GITIGNORE.read().await.clone()
162}
163
164pub async fn should_exclude_file(file_path: &Path) -> bool {
166 let gitignore = snapshot_global_vtcode_gitignore().await;
167 gitignore.should_exclude(file_path)
168}
169
170pub async fn filter_paths(paths: Vec<PathBuf>) -> Vec<PathBuf> {
172 let gitignore = snapshot_global_vtcode_gitignore().await;
173 gitignore.filter_paths(paths)
174}
175
176pub async fn reload_vtcode_gitignore() -> Result<()> {
178 initialize_vtcode_gitignore().await
179}