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

Function build_hunks_from_diff

atomic-repository/src/diff_engine.rs:1230–1399  ·  view source on GitHub ↗
(
    diff_result: &DiffResult,
    old_lines: &[&[u8]],
    new_lines: &[&[u8]],
    context: usize,
)

Source from the content-addressed store, hash-verified

1228}
1229
1230pub fn build_hunks_from_diff(
1231 diff_result: &DiffResult,
1232 old_lines: &[&[u8]],
1233 new_lines: &[&[u8]],
1234 context: usize,
1235) -> Vec<DiffHunk> {
1236 let mut hunks = Vec::new();
1237
1238 // Simple implementation: create one graph_op for all changes
1239 // A more sophisticated implementation would group changes by proximity
1240
1241 let mut current_hunk: Option<DiffHunk> = None;
1242 let mut old_line = 1;
1243 let mut new_line = 1;
1244
1245 for op in diff_result.iter() {
1246 match op {
1247 DiffOp::Equal { len, .. } => {
1248 if let Some(ref mut graph_op) = current_hunk {
1249 // Add context lines to current graph_op (up to context limit)
1250 let context_count = cmp::min(*len, context);
1251 for i in 0..context_count {
1252 let content = if new_line - 1 + i < new_lines.len() {
1253 String::from_utf8_lossy(new_lines[new_line - 1 + i]).into_owned()
1254 } else {
1255 String::new()
1256 };
1257 graph_op.add_line(HunkLine::context(content, old_line + i, new_line + i));
1258 }
1259
1260 // If we've shown enough context and there's more equal content,
1261 // close this graph_op
1262 if *len > context * 2 {
1263 graph_op.old_count = graph_op
1264 .lines
1265 .iter()
1266 .filter(|l| l.old_line_num.is_some())
1267 .count();
1268 graph_op.new_count = graph_op
1269 .lines
1270 .iter()
1271 .filter(|l| l.new_line_num.is_some())
1272 .count();
1273 hunks.push(current_hunk.take().unwrap());
1274 }
1275 }
1276 old_line += len;
1277 new_line += len;
1278 }
1279 DiffOp::Insert { len, .. } => {
1280 // Start a new graph_op if we don't have one
1281 if current_hunk.is_none() {
1282 let old_start = old_line.saturating_sub(context).max(1);
1283 let new_start = new_line.saturating_sub(context).max(1);
1284 current_hunk = Some(DiffHunk::new(old_start, 0, new_start, 0));
1285
1286 // Add leading context
1287 let context_start = new_line.saturating_sub(context);

Callers 3

change_file_diffsFunction · 0.70
change_file_diffs_withFunction · 0.70

Calls 10

contextFunction · 0.85
addedFunction · 0.85
removedFunction · 0.85
is_noneMethod · 0.80
iterMethod · 0.45
lenMethod · 0.45
add_lineMethod · 0.45
countMethod · 0.45
pushMethod · 0.45
unwrapMethod · 0.45

Tested by

no test coverage detected