Skip to main content

arbor_watcher/
indexer.rs

1//! Directory indexing.
2//!
3//! Walks directories to find and parse source files, building
4//! the initial code graph.
5
6use arbor_core::{parse_file, CodeNode};
7use arbor_graph::{ArborGraph, GraphBuilder, GraphStore};
8use ignore::WalkBuilder;
9use std::collections::HashSet;
10use std::path::{Path, PathBuf};
11use std::time::Instant;
12use tracing::{debug, info, warn};
13
14/// Result of indexing a directory.
15pub struct IndexResult {
16    /// The built graph.
17    pub graph: ArborGraph,
18
19    /// Number of files processed (parsed fresh).
20    pub files_indexed: usize,
21
22    /// Number of files loaded from cache.
23    pub cache_hits: usize,
24
25    /// Number of nodes extracted.
26    pub nodes_extracted: usize,
27
28    /// Time taken in milliseconds.
29    pub duration_ms: u64,
30
31    /// Files that failed to parse.
32    pub errors: Vec<(String, String)>,
33}
34
35/// Options for directory indexing.
36#[derive(Debug, Clone, Default)]
37pub struct IndexOptions {
38    /// Follow symbolic links when walking directories.
39    pub follow_symlinks: bool,
40
41    /// Path to cache directory (e.g., `.arbor/cache`).
42    /// If None, caching is disabled.
43    pub cache_path: Option<PathBuf>,
44}
45
46/// Indexes a directory and returns the code graph.
47///
48/// This walks all source files, parses them, and builds the
49/// relationship graph. It respects .gitignore patterns.
50///
51/// If `options.cache_path` is set, files are cached with their mtimes.
52/// Only files with changed mtimes are re-parsed.
53///
54/// # Example
55///
56/// ```no_run
57/// use arbor_watcher::{index_directory, IndexOptions};
58/// use std::path::Path;
59///
60/// let result = index_directory(Path::new("./src"), IndexOptions::default()).unwrap();
61/// println!("Indexed {} files, {} nodes", result.files_indexed, result.nodes_extracted);
62/// ```
63pub fn index_directory(root: &Path, options: IndexOptions) -> Result<IndexResult, std::io::Error> {
64    let start = Instant::now();
65    let mut builder = GraphBuilder::new();
66    let mut files_indexed = 0;
67    let mut cache_hits = 0;
68    let mut nodes_extracted = 0;
69    let mut errors = Vec::new();
70
71    info!("Starting index of {}", root.display());
72
73    // Open cache if configured
74    let store =
75        options
76            .cache_path
77            .as_ref()
78            .and_then(|path| match GraphStore::open_or_reset(path) {
79                Ok(s) => Some(s),
80                Err(e) => {
81                    warn!("Failed to open cache: {}, proceeding without cache", e);
82                    None
83                }
84            });
85
86    // Track files we've seen (for detecting deleted files)
87    let mut seen_files: HashSet<String> = HashSet::new();
88
89    // Walk the directory, respecting .gitignore
90    let walker = WalkBuilder::new(root)
91        .hidden(true) // Skip hidden files
92        .git_ignore(true) // Respect .gitignore
93        .git_global(true)
94        .git_exclude(true)
95        .follow_links(options.follow_symlinks)
96        .build();
97
98    for entry in walker.filter_map(Result::ok) {
99        let path = entry.path();
100
101        // Skip directories
102        if path.is_dir() {
103            continue;
104        }
105
106        // Check if it's a supported file type
107        let extension = match path.extension().and_then(|e| e.to_str()) {
108            Some(ext) => ext,
109            None => continue,
110        };
111
112        if !arbor_core::languages::is_supported(extension) {
113            continue;
114        }
115
116        let path_str = path.display().to_string();
117        seen_files.insert(path_str.clone());
118
119        // Check cache
120        if let Some(ref store) = store {
121            // Get file mtime
122            let current_mtime = match std::fs::metadata(path) {
123                Ok(meta) => meta
124                    .modified()
125                    .ok()
126                    .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
127                    .map(|d| d.as_secs())
128                    .unwrap_or(0),
129                Err(_) => 0,
130            };
131
132            // Check cached mtime
133            if let Ok(Some(cached_mtime)) = store.get_mtime(&path_str) {
134                if cached_mtime == current_mtime {
135                    // File unchanged, load from cache
136                    if let Ok(Some(cached_nodes)) = store.get_file_nodes(&path_str) {
137                        debug!("Cache hit: {}", path.display());
138                        nodes_extracted += cached_nodes.len();
139                        cache_hits += 1;
140                        builder.add_nodes(cached_nodes);
141                        continue;
142                    }
143                }
144            }
145
146            // Cache miss or stale, parse file
147            debug!("Parsing (cache miss): {}", path.display());
148            match parse_file(path) {
149                Ok(nodes) => {
150                    nodes_extracted += nodes.len();
151                    files_indexed += 1;
152                    // Update cache
153                    if let Err(e) = store.update_file(&path_str, &nodes, current_mtime) {
154                        warn!("Failed to update cache for {}: {}", path_str, e);
155                    }
156                    builder.add_nodes(nodes);
157                }
158                Err(e) => {
159                    warn!("Failed to parse {}: {}", path.display(), e);
160                    errors.push((path_str, e.to_string()));
161                }
162            }
163        } else {
164            // No cache, parse directly
165            debug!("Parsing {}", path.display());
166            match parse_file(path) {
167                Ok(nodes) => {
168                    nodes_extracted += nodes.len();
169                    files_indexed += 1;
170                    builder.add_nodes(nodes);
171                }
172                Err(e) => {
173                    warn!("Failed to parse {}: {}", path.display(), e);
174                    errors.push((path_str, e.to_string()));
175                }
176            }
177        }
178    }
179
180    // Handle deleted files: remove from cache any files that no longer exist
181    if let Some(ref store) = store {
182        if let Ok(cached_files) = store.list_cached_files() {
183            for cached_file in cached_files {
184                if !seen_files.contains(&cached_file) {
185                    debug!("Removing deleted file from cache: {}", cached_file);
186                    if let Err(e) = store.remove_file(&cached_file) {
187                        warn!("Failed to remove {} from cache: {}", cached_file, e);
188                    }
189                }
190            }
191        }
192    }
193
194    let graph = builder.build();
195    let duration = start.elapsed();
196
197    info!(
198        "Indexed {} files, {} cache hits ({} nodes) in {:?}",
199        files_indexed, cache_hits, nodes_extracted, duration
200    );
201
202    Ok(IndexResult {
203        graph,
204        files_indexed,
205        cache_hits,
206        nodes_extracted,
207        duration_ms: duration.as_millis() as u64,
208        errors,
209    })
210}
211
212/// Parses a single file and returns its nodes.
213#[allow(dead_code)]
214pub fn parse_single_file(path: &Path) -> Result<Vec<CodeNode>, arbor_core::ParseError> {
215    parse_file(path)
216}
217
218/// Returns true if any supported source file under `root` is newer than `cache_mtime`.
219///
220/// Used by read commands to detect a stale `graph.bin` before trusting it.
221/// Walks the same gitignore-respecting tree as [`index_directory`] but only
222/// stats files — no parsing — and early-exits on the first newer file.
223///
224/// `cache_mtime` is the modified time of the cache file, in seconds since the
225/// UNIX epoch. Catches edits and additions; a lone deletion leaves no newer
226/// file, so it is picked up on the next edit instead.
227pub fn sources_newer_than(root: &Path, cache_mtime: u64, follow_symlinks: bool) -> bool {
228    let walker = WalkBuilder::new(root)
229        .hidden(true)
230        .git_ignore(true)
231        .git_global(true)
232        .git_exclude(true)
233        .follow_links(follow_symlinks)
234        .build();
235
236    for entry in walker.filter_map(Result::ok) {
237        let path = entry.path();
238        if path.is_dir() {
239            continue;
240        }
241        match path.extension().and_then(|e| e.to_str()) {
242            Some(ext) if arbor_core::languages::is_supported(ext) => {}
243            _ => continue,
244        }
245        let mtime = match std::fs::metadata(path).and_then(|m| m.modified()) {
246            Ok(t) => t
247                .duration_since(std::time::UNIX_EPOCH)
248                .map(|d| d.as_secs())
249                .unwrap_or(0),
250            Err(_) => continue,
251        };
252        if mtime > cache_mtime {
253            return true;
254        }
255    }
256    false
257}
258
259#[cfg(test)]
260mod tests {
261    use super::*;
262    use std::fs;
263    use tempfile::tempdir;
264
265    #[test]
266    fn test_index_empty_directory() {
267        let dir = tempdir().unwrap();
268        let result = index_directory(dir.path(), IndexOptions::default()).unwrap();
269        assert_eq!(result.files_indexed, 0);
270        assert_eq!(result.nodes_extracted, 0);
271    }
272
273    #[test]
274    fn test_sources_newer_than_detects_fresh_edit() {
275        let dir = tempdir().unwrap();
276        fs::write(dir.path().join("a.rs"), "pub fn a() {}").unwrap();
277
278        // Cache mtime far in the past → the source file is newer → stale.
279        assert!(sources_newer_than(dir.path(), 0, false));
280
281        // Cache mtime far in the future → nothing newer → fresh.
282        let far_future = u64::MAX;
283        assert!(!sources_newer_than(dir.path(), far_future, false));
284    }
285
286    #[test]
287    fn test_sources_newer_than_ignores_unsupported_files() {
288        let dir = tempdir().unwrap();
289        // Only an unsupported file exists; it must not mark the cache stale.
290        fs::write(dir.path().join("notes.txt"), "hello").unwrap();
291        assert!(!sources_newer_than(dir.path(), 0, false));
292    }
293
294    #[test]
295    fn test_index_with_rust_file() {
296        let dir = tempdir().unwrap();
297        let file_path = dir.path().join("test.rs");
298
299        fs::write(
300            &file_path,
301            r#"
302            pub fn hello() {
303                println!("Hello!");
304            }
305        "#,
306        )
307        .unwrap();
308
309        let result = index_directory(dir.path(), IndexOptions::default()).unwrap();
310        assert_eq!(result.files_indexed, 1);
311        assert!(result.nodes_extracted > 0);
312    }
313
314    /// Helper to create a directory symlink cross-platform.
315    /// Returns None if symlink creation fails (e.g., no privileges on Windows).
316    fn create_dir_symlink(original: &std::path::Path, link: &std::path::Path) -> Option<()> {
317        #[cfg(unix)]
318        {
319            std::os::unix::fs::symlink(original, link).ok()
320        }
321        #[cfg(windows)]
322        {
323            std::os::windows::fs::symlink_dir(original, link).ok()
324        }
325        #[cfg(not(any(unix, windows)))]
326        {
327            None
328        }
329    }
330
331    #[test]
332    fn test_index_does_not_follow_symlinks_by_default() {
333        let dir = tempdir().unwrap();
334        let linked_dir = tempdir().unwrap();
335
336        // Create a file in the linked directory
337        let linked_file = linked_dir.path().join("linked.rs");
338        fs::write(&linked_file, "pub fn linked_func() {}").unwrap();
339
340        // Create a symlink to the linked directory
341        let symlink_path = dir.path().join("linked");
342        if create_dir_symlink(linked_dir.path(), &symlink_path).is_none() {
343            // Skip test if symlinks not supported (e.g., Windows without privileges)
344            return;
345        }
346
347        // Index without following symlinks (default)
348        let result = index_directory(dir.path(), IndexOptions::default()).unwrap();
349        assert_eq!(result.files_indexed, 0);
350    }
351
352    #[test]
353    fn test_index_follows_symlinks_when_enabled() {
354        let dir = tempdir().unwrap();
355        let linked_dir = tempdir().unwrap();
356
357        // Create a file in the linked directory
358        let linked_file = linked_dir.path().join("linked.rs");
359        fs::write(&linked_file, "pub fn linked_func() {}").unwrap();
360
361        // Create a symlink to the linked directory
362        let symlink_path = dir.path().join("linked");
363        if create_dir_symlink(linked_dir.path(), &symlink_path).is_none() {
364            // Skip test if symlinks not supported (e.g., Windows without privileges)
365            return;
366        }
367
368        // Index with follow_symlinks enabled
369        let options = IndexOptions {
370            follow_symlinks: true,
371            cache_path: None,
372        };
373        let result = index_directory(dir.path(), options).unwrap();
374        assert_eq!(result.files_indexed, 1);
375        assert!(result.nodes_extracted > 0);
376    }
377}