MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / file_diff_from_chunk

Function file_diff_from_chunk

atomic-cli/src/commands/rpc.rs:2224–2282  ·  view source on GitHub ↗

Rebuild one local-path `FileDiff` from a wire `DiffChunk`. The chunk carries the file's status plus its recorded and working-copy contents (see DiffChunk.status/old_content/new_content), so the exact comparison the local path performs — `diff_text` + hunks with context — runs here and feeds the shared formatters.

(
    chunk: &pb::DiffChunk,
    algorithm: &atomic_core::diff::Algorithm,
    context_lines: usize,
)

Source from the content-addressed store, hash-verified

2222/// comparison the local path performs — `diff_text` + hunks with context —
2223/// runs here and feeds the shared formatters.
2224fn file_diff_from_chunk(
2225 chunk: &pb::DiffChunk,
2226 algorithm: &atomic_core::diff::Algorithm,
2227 context_lines: usize,
2228) -> Option<super::diff::types::FileDiff> {
2229 use super::diff::types::{FileChangeStatus, FileDiff};
2230 use atomic_core::diff::diff_text;
2231
2232 let status = match chunk.status.as_deref() {
2233 Some("added") => FileChangeStatus::Added,
2234 Some("deleted") => FileChangeStatus::Deleted,
2235 Some("untracked") => FileChangeStatus::Untracked,
2236 Some("typechanged") => FileChangeStatus::TypeChanged,
2237 _ => FileChangeStatus::Modified,
2238 };
2239
2240 let mut file_diff = match status {
2241 FileChangeStatus::Added => FileDiff::added(&chunk.path),
2242 FileChangeStatus::Deleted => FileDiff::deleted(&chunk.path),
2243 FileChangeStatus::Untracked => FileDiff::new(&chunk.path, FileChangeStatus::Untracked),
2244 _ => FileDiff::new(&chunk.path, status),
2245 };
2246
2247 if chunk.binary {
2248 file_diff.is_binary = true;
2249 file_diff.stats.status = status.status_char();
2250 return Some(file_diff);
2251 }
2252
2253 let old_content = chunk.old_content.clone().unwrap_or_default();
2254 let new_content = chunk.new_content.clone().unwrap_or_default();
2255
2256 // stat-only: the wire carries the counts but not the contents (the
2257 // server omits them) — a stats-only entry renders `--stat` fine.
2258 if chunk.patch.is_none() {
2259 file_diff.stats.insertions = chunk.additions as usize;
2260 file_diff.stats.deletions = chunk.deletions as usize;
2261 return Some(file_diff);
2262 }
2263
2264 let diff_result = diff_text(&old_content, &new_content, *algorithm);
2265 if diff_result.is_unchanged()
2266 && !matches!(
2267 chunk.status.as_deref(),
2268 Some("typechanged" | "permissionschanged")
2269 )
2270 {
2271 return None;
2272 }
2273 let old_lines: Vec<_> = old_content.split(|&b| b == b'\n').collect();
2274 let new_lines: Vec<_> = new_content.split(|&b| b == b'\n').collect();
2275 for hunk in
2276 super::diff::build_hunks_from_diff(&diff_result, &old_lines, &new_lines, context_lines)
2277 {
2278 file_diff.add_hunk(hunk);
2279 }
2280 file_diff.compute_stats();
2281 Some(file_diff)

Callers 2

diffFunction · 0.85
diff_view_pairFunction · 0.85

Calls 11

addedFunction · 0.85
deletedFunction · 0.85
diff_textFunction · 0.85
is_noneMethod · 0.80
build_hunks_from_diffFunction · 0.50
status_charMethod · 0.45
cloneMethod · 0.45
is_unchangedMethod · 0.45
collectMethod · 0.45
add_hunkMethod · 0.45
compute_statsMethod · 0.45

Tested by

no test coverage detected