Skip to main content

java_diff_utils_rs/patch/
conflict_formatter.rs

1use super::delta::Delta;
2use super::error::PatchError;
3use super::verify_chunk::VerifyChunk;
4
5pub fn conflict_produces_merge_conflict<T>(
6    _verify: VerifyChunk,
7    delta: &Delta<T>,
8    result: &mut Vec<T>,
9) -> Result<(), PatchError>
10where
11    T: Clone + From<&'static str>,
12{
13    let pos = delta.source().position();
14    let src_len = delta.source().len();
15
16    if result.len() > pos {
17        // Remove the actual content currently at the delta's source position
18        // (this may differ from what the delta expected, which is why we're here).
19        let end = (pos + src_len).min(result.len());
20        let actual: Vec<T> = result.splice(pos..end, std::iter::empty()).collect();
21
22        // Build the merge-conflict block: actual content vs. the patch's
23        // original (source) content. The target/revised replacement is
24        // intentionally never inserted here, matching upstream behavior.
25        let mut org_data: Vec<T> = Vec::with_capacity(actual.len() + src_len + 3);
26        org_data.push(T::from("<<<<<< HEAD"));
27        org_data.extend(actual);
28        org_data.push(T::from("======"));
29        org_data.extend(delta.source().lines().iter().cloned());
30        org_data.push(T::from(">>>>>>> PATCH"));
31
32        result.splice(pos..pos, org_data);
33
34        Ok(())
35    } else {
36        Err(PatchError::PatchFailed("Not supported yet.".into()))
37    }
38}