Process an added file: read content, detect encoding, create hunks, tokenize.
(
input: &FileRecordInput,
options: &RecordingOptions,
)
| 42 | |
| 43 | /// Process an added file: read content, detect encoding, create hunks, tokenize. |
| 44 | fn process_added_file( |
| 45 | input: &FileRecordInput, |
| 46 | options: &RecordingOptions, |
| 47 | ) -> Result<FileRecordOutput, String> { |
| 48 | // Read the file content from disk |
| 49 | let content = std::fs::read(&input.full_path) |
| 50 | .map_err(|e| format!("Failed to read {}: {}", input.path, e))?; |
| 51 | |
| 52 | // Check size limits |
| 53 | if options.exceeds_max_size(content.len()) { |
| 54 | if options.get_skip_binary() { |
| 55 | return Ok(FileRecordOutput::skipped( |
| 56 | input.path.clone(), |
| 57 | FileRecordKind::Added, |
| 58 | )); |
| 59 | } else { |
| 60 | return Err(format!( |
| 61 | "File {} exceeds maximum size ({} bytes)", |
| 62 | input.path, |
| 63 | content.len(), |
| 64 | )); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Create a per-file memory working copy (no shared state) |
| 69 | let memory_wc = Memory::new(); |
| 70 | memory_wc.add_file(&input.path, &content); |
| 71 | |
| 72 | let detected = DetectedFile::added(&input.path); |
| 73 | |
| 74 | match record_added_file(&memory_wc, &detected, options) { |
| 75 | Ok(recorded) => { |
| 76 | if recorded.is_empty() { |
| 77 | return Ok(FileRecordOutput::skipped( |
| 78 | input.path.clone(), |
| 79 | FileRecordKind::Added, |
| 80 | )); |
| 81 | } |
| 82 | |
| 83 | let crdt_stats = recorded.crdt_stats(); |
| 84 | let file_stats = FileRecordStats { |
| 85 | hunks_created: recorded.hunk_count(), |
| 86 | content_bytes: recorded.content_len() as u64, |
| 87 | vertices_added: 3, // name + inode + content |
| 88 | lines_added: crdt_stats.map_or(0, |s| s.lines_added), |
| 89 | lines_deleted: crdt_stats.map_or(0, |s| s.lines_deleted), |
| 90 | lines_modified: crdt_stats.map_or(0, |s| s.lines_modified), |
| 91 | tokens_added: crdt_stats.map_or(0, |s| s.tokens_added), |
| 92 | tokens_deleted: crdt_stats.map_or(0, |s| s.tokens_deleted), |
| 93 | tokens_replaced: crdt_stats.map_or(0, |s| s.tokens_replaced), |
| 94 | ..FileRecordStats::default() |
| 95 | }; |
| 96 | |
| 97 | Ok(FileRecordOutput { |
| 98 | path: input.path.clone(), |
| 99 | kind: FileRecordKind::Added, |
| 100 | recorded: Some(recorded), |
| 101 | skipped: false, |
no test coverage detected