Skip to main content

vtcode_commons/
vtcodegitignore.rs

1//! .vtcodegitignore file pattern matching utilities
2//!
3//! Uses the `ignore` crate's gitignore parser for correct, battle-tested
4//! pattern matching instead of hand-rolled glob conversion.
5
6use anyhow::{Result, anyhow};
7use ignore::gitignore::{Gitignore, GitignoreBuilder};
8use std::path::{Path, PathBuf};
9use std::sync::Arc;
10use tokio::fs;
11
12/// Represents a .vtcodegitignore file with pattern matching capabilities
13#[derive(Debug, Clone)]
14pub struct VTCodeGitignore {
15    /// Root directory where .vtcodegitignore was found
16    root_dir: PathBuf,
17    /// Compiled gitignore matcher
18    matcher: Gitignore,
19    /// Whether the .vtcodegitignore file exists and was loaded
20    loaded: bool,
21}
22
23impl VTCodeGitignore {
24    /// Create a new VTCodeGitignore instance by looking for .vtcodegitignore in the current directory
25    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(&current_dir).await
30    }
31
32    /// Create a VTCodeGitignore instance from a specific directory
33    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                    // Log warning but don't fail - just treat as no patterns
46                    tracing::warn!("Failed to load .vtcodegitignore: {}", e);
47                }
48            }
49        }
50
51        let matcher = builder.build().unwrap_or_else(|_| {
52            // Fallback to empty matcher on build error
53            Gitignore::empty()
54        });
55
56        Ok(Self {
57            root_dir: root_dir.to_path_buf(),
58            matcher,
59            loaded,
60        })
61    }
62
63    /// Load patterns from the .vtcodegitignore file into the builder
64    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            // Skip empty lines and comments
73            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    /// Check if a file path should be excluded based on the .vtcodegitignore patterns
91    pub fn should_exclude(&self, file_path: &Path) -> bool {
92        if !self.loaded {
93            return false;
94        }
95
96        // Convert to relative path from the root directory
97        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    /// Filter a list of file paths based on .vtcodegitignore patterns
108    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    /// Check if the .vtcodegitignore file was loaded successfully
120    pub fn is_loaded(&self) -> bool {
121        self.loaded
122    }
123
124    /// Get the number of patterns loaded
125    pub fn pattern_count(&self) -> usize {
126        self.matcher.num_ignores() as usize
127    }
128
129    /// Get the root directory
130    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
147/// Global .vtcodegitignore instance for easy access
148pub 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
151/// Initialize the global .vtcodegitignore instance
152pub 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
159/// Snapshot the global .vtcodegitignore instance.
160pub async fn snapshot_global_vtcode_gitignore() -> Arc<VTCodeGitignore> {
161    VTCODE_GITIGNORE.read().await.clone()
162}
163
164/// Check if a file should be excluded by the global .vtcodegitignore
165pub 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
170/// Filter paths using the global .vtcodegitignore
171pub async fn filter_paths(paths: Vec<PathBuf>) -> Vec<PathBuf> {
172    let gitignore = snapshot_global_vtcode_gitignore().await;
173    gitignore.filter_paths(paths)
174}
175
176/// Reload the global .vtcodegitignore from disk
177pub async fn reload_vtcode_gitignore() -> Result<()> {
178    initialize_vtcode_gitignore().await
179}