gix_merge/blob/builtin_driver/text/
function.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use crate::blob::builtin_driver::text::utils::{
    assure_ends_with_nl, contains_lines, detect_line_ending, detect_line_ending_or_nl, fill_ancestor,
    hunks_differ_in_diff3, take_intersecting, tokens, write_ancestor, write_conflict_marker, write_hunks,
    zealously_contract_hunks, CollectHunks, Hunk, Side,
};
use crate::blob::builtin_driver::text::{Conflict, ConflictStyle, Labels, Options};
use crate::blob::Resolution;
use std::ops::Range;

/// Merge `current` and `other` with `ancestor` as base according to `opts`.
///
/// Use `labels` to annotate conflict sections.
///
/// `input` is for reusing memory for lists of tokens, but note that it grows indefinitely
/// while tokens for `current`, `ancestor` and `other` are added.
/// Place the merged result in `out` (cleared before use) and return the resolution.
///
/// # Important
///
/// *The caller* is responsible for clearing `input`, otherwise tokens will accumulate.
/// This idea is to save time if the input is known to be very similar.
#[allow(clippy::too_many_arguments)]
pub fn merge<'a>(
    out: &mut Vec<u8>,
    input: &mut imara_diff::intern::InternedInput<&'a [u8]>,
    Labels {
        ancestor: ancestor_label,
        current: current_label,
        other: other_label,
    }: Labels<'_>,
    current: &'a [u8],
    ancestor: &'a [u8],
    other: &'a [u8],
    Options {
        diff_algorithm,
        conflict,
    }: Options,
) -> Resolution {
    out.clear();
    input.update_before(tokens(ancestor));
    input.update_after(tokens(current));

    let hunks = imara_diff::diff(
        diff_algorithm,
        input,
        CollectHunks {
            side: Side::Current,
            hunks: Default::default(),
        },
    );

    let current_tokens = std::mem::take(&mut input.after);
    input.update_after(tokens(other));

    let mut hunks = imara_diff::diff(
        diff_algorithm,
        input,
        CollectHunks {
            side: Side::Other,
            hunks,
        },
    );

    if hunks.is_empty() {
        write_ancestor(input, 0, input.before.len(), out);
        return Resolution::Complete;
    }

    hunks.sort_by(|a, b| a.before.start.cmp(&b.before.start));
    let mut hunks = hunks.into_iter().peekable();
    let mut intersecting = Vec::new();
    let mut ancestor_integrated_until = 0;
    let mut resolution = Resolution::Complete;
    let mut current_hunks = Vec::with_capacity(2);
    while take_intersecting(&mut hunks, &mut current_hunks, &mut intersecting).is_some() {
        if intersecting.is_empty() {
            let hunk = current_hunks.pop().expect("always pushed during intersection check");
            write_ancestor(input, ancestor_integrated_until, hunk.before.start as usize, out);
            ancestor_integrated_until = hunk.before.end;
            write_hunks(std::slice::from_ref(&hunk), input, &current_tokens, out);
            continue;
        }

        let filled_hunks_side = current_hunks.first().expect("at least one hunk").side;
        {
            let filled_hunks_range = before_range_from_hunks(&current_hunks);
            let intersecting_range = before_range_from_hunks(&intersecting);
            let extended_range = filled_hunks_range.start..intersecting_range.end.max(filled_hunks_range.end);
            fill_ancestor(&extended_range, &mut current_hunks);
            fill_ancestor(&extended_range, &mut intersecting);
        }
        match conflict {
            Conflict::Keep { style, marker_size } => {
                let marker_size = marker_size.get();
                let (hunks_front_and_back, num_hunks_front) = match style {
                    ConflictStyle::Merge | ConflictStyle::ZealousDiff3 => {
                        zealously_contract_hunks(&mut current_hunks, &mut intersecting, input, &current_tokens)
                    }
                    ConflictStyle::Diff3 => (Vec::new(), 0),
                };
                let (our_hunks, their_hunks) = match filled_hunks_side {
                    Side::Current => (&current_hunks, &intersecting),
                    Side::Other => (&intersecting, &current_hunks),
                    Side::Ancestor => {
                        unreachable!("initial hunks are never ancestors")
                    }
                };
                let (front_hunks, back_hunks) = hunks_front_and_back.split_at(num_hunks_front);
                let first_hunk = first_hunk(front_hunks, our_hunks, their_hunks, back_hunks);
                let last_hunk = last_hunk(front_hunks, our_hunks, their_hunks, back_hunks);
                write_ancestor(input, ancestor_integrated_until, first_hunk.before.start as usize, out);
                write_hunks(front_hunks, input, &current_tokens, out);
                if their_hunks.is_empty() {
                    write_hunks(our_hunks, input, &current_tokens, out);
                } else if our_hunks.is_empty() {
                    write_hunks(their_hunks, input, &current_tokens, out);
                } else {
                    // DEVIATION: this makes tests (mostly) pass, but probably is very different from what Git does.
                    let hunk_storage;
                    let nl = detect_line_ending(
                        if front_hunks.is_empty() {
                            hunk_storage = Hunk {
                                before: ancestor_integrated_until..first_hunk.before.start,
                                after: Default::default(),
                                side: Side::Ancestor,
                            };
                            std::slice::from_ref(&hunk_storage)
                        } else {
                            front_hunks
                        },
                        input,
                        &current_tokens,
                    )
                    .or_else(|| detect_line_ending(our_hunks, input, &current_tokens))
                    .unwrap_or(b"\n".into());
                    match style {
                        ConflictStyle::Merge => {
                            if contains_lines(our_hunks) || contains_lines(their_hunks) {
                                resolution = Resolution::Conflict;
                                write_conflict_marker(out, b'<', current_label, marker_size, nl);
                                write_hunks(our_hunks, input, &current_tokens, out);
                                write_conflict_marker(out, b'=', None, marker_size, nl);
                                write_hunks(their_hunks, input, &current_tokens, out);
                                write_conflict_marker(out, b'>', other_label, marker_size, nl);
                            }
                        }
                        ConflictStyle::Diff3 | ConflictStyle::ZealousDiff3 => {
                            if contains_lines(our_hunks) || contains_lines(their_hunks) {
                                if hunks_differ_in_diff3(style, our_hunks, their_hunks, input, &current_tokens) {
                                    resolution = Resolution::Conflict;
                                    write_conflict_marker(out, b'<', current_label, marker_size, nl);
                                    write_hunks(our_hunks, input, &current_tokens, out);
                                    let ancestor_hunk = Hunk {
                                        before: first_hunk.before.start..last_hunk.before.end,
                                        after: Default::default(),
                                        side: Side::Ancestor,
                                    };
                                    let ancestor_hunk = std::slice::from_ref(&ancestor_hunk);
                                    let ancestor_nl = detect_line_ending_or_nl(ancestor_hunk, input, &current_tokens);
                                    write_conflict_marker(out, b'|', ancestor_label, marker_size, ancestor_nl);
                                    write_hunks(ancestor_hunk, input, &current_tokens, out);
                                    write_conflict_marker(out, b'=', None, marker_size, nl);
                                    write_hunks(their_hunks, input, &current_tokens, out);
                                    write_conflict_marker(out, b'>', other_label, marker_size, nl);
                                } else {
                                    write_hunks(our_hunks, input, &current_tokens, out);
                                }
                            }
                        }
                    }
                }
                write_hunks(back_hunks, input, &current_tokens, out);
                ancestor_integrated_until = last_hunk.before.end;
            }
            Conflict::ResolveWithOurs | Conflict::ResolveWithTheirs => {
                let (our_hunks, their_hunks) = match filled_hunks_side {
                    Side::Current => (&current_hunks, &intersecting),
                    Side::Other => (&intersecting, &current_hunks),
                    Side::Ancestor => {
                        unreachable!("initial hunks are never ancestors")
                    }
                };
                if hunks_differ_in_diff3(ConflictStyle::Diff3, our_hunks, their_hunks, input, &current_tokens) {
                    resolution = Resolution::CompleteWithAutoResolvedConflict;
                }
                let hunks_to_write = if conflict == Conflict::ResolveWithOurs {
                    our_hunks
                } else {
                    their_hunks
                };
                if let Some(first_hunk) = hunks_to_write.first() {
                    write_ancestor(input, ancestor_integrated_until, first_hunk.before.start as usize, out);
                }
                write_hunks(hunks_to_write, input, &current_tokens, out);
                if let Some(last_hunk) = hunks_to_write.last() {
                    ancestor_integrated_until = last_hunk.before.end;
                }
            }
            Conflict::ResolveWithUnion => {
                let (hunks_front_and_back, num_hunks_front) =
                    zealously_contract_hunks(&mut current_hunks, &mut intersecting, input, &current_tokens);

                let (our_hunks, their_hunks) = match filled_hunks_side {
                    Side::Current => (&current_hunks, &intersecting),
                    Side::Other => (&intersecting, &current_hunks),
                    Side::Ancestor => {
                        unreachable!("initial hunks are never ancestors")
                    }
                };
                if hunks_differ_in_diff3(ConflictStyle::Diff3, our_hunks, their_hunks, input, &current_tokens) {
                    resolution = Resolution::CompleteWithAutoResolvedConflict;
                }
                let (front_hunks, back_hunks) = hunks_front_and_back.split_at(num_hunks_front);
                let first_hunk = first_hunk(front_hunks, our_hunks, their_hunks, back_hunks);
                write_ancestor(input, ancestor_integrated_until, first_hunk.before.start as usize, out);
                write_hunks(front_hunks, input, &current_tokens, out);
                assure_ends_with_nl(out, detect_line_ending_or_nl(front_hunks, input, &current_tokens));
                write_hunks(our_hunks, input, &current_tokens, out);
                assure_ends_with_nl(out, detect_line_ending_or_nl(our_hunks, input, &current_tokens));
                write_hunks(their_hunks, input, &current_tokens, out);
                if !back_hunks.is_empty() {
                    assure_ends_with_nl(out, detect_line_ending_or_nl(their_hunks, input, &current_tokens));
                }
                write_hunks(back_hunks, input, &current_tokens, out);
                let last_hunk = last_hunk(front_hunks, our_hunks, their_hunks, back_hunks);
                ancestor_integrated_until = last_hunk.before.end;
            }
        }
    }
    write_ancestor(input, ancestor_integrated_until, input.before.len(), out);

    resolution
}

fn first_hunk<'a>(front: &'a [Hunk], ours: &'a [Hunk], theirs: &'a [Hunk], back: &'a [Hunk]) -> &'a Hunk {
    front
        .first()
        .or(ours.first())
        .or(theirs.first())
        .or(back.first())
        .expect("at least one hunk - we aborted if there are none anywhere")
}

/// Note that last-hunk could be [`first_hunk()`], so the hunk must only be used accordingly.
fn last_hunk<'a>(front: &'a [Hunk], ours: &'a [Hunk], theirs: &'a [Hunk], back: &'a [Hunk]) -> &'a Hunk {
    back.last()
        .or(theirs.last())
        .or(ours.last())
        .or(front.last())
        .expect("at least one hunk - we aborted if there are none anywhere")
}

fn before_range_from_hunks(hunks: &[Hunk]) -> Range<u32> {
    hunks
        .first()
        .zip(hunks.last())
        .map(|(f, l)| f.before.start..l.before.end)
        .expect("at least one entry")
}