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

Function file_diff_from_chunk

atomic-cli/src/commands/rpc.rs:2225–2283  ·  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

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

Callers 2

diffFunction · 0.85
diff_view_pairFunction · 0.85

Calls 11

deletedFunction · 0.85
diff_textFunction · 0.85
is_noneMethod · 0.80
addedFunction · 0.50
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