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
//!
//! The Interdiff library.
//!

use std::collections::VecDeque;

use log::*;

use patch_rs::Patch;

pub fn interdiff(mut patch_1: Patch, mut patch_2: Patch, context_radius: usize) -> Patch {
    let mut interdiff = Patch {
        input: patch_1.output.to_owned(),
        output: patch_2.output.to_owned(),
        contexts: VecDeque::new(),
    };
    let mut patch_1_offset = 0;
    let mut patch_2_offset = 0;

    trace!("DRAINING BOTH PATCHES");
    while !patch_1.contexts.is_empty() && !patch_2.contexts.is_empty() {
        let p1 = patch_1.contexts.front().unwrap();
        let p2 = patch_2.contexts.front().unwrap();
        if p1.header.file1_l <= p2.header.file1_l {
            let context = patch_1.contexts.pop_front().unwrap();
            let flipped = context.flip(context_radius);
            for mut context in flipped.into_iter() {
                context.shift(patch_2_offset);
                patch_1_offset += context.offset();
                interdiff.contexts.push_back(context);
            }
        } else {
            let context = patch_2.contexts.pop_front().unwrap();
            let reduced = context.reduce(context_radius);
            for mut context in reduced.into_iter() {
                context.shift(-patch_1_offset);
                patch_2_offset += context.offset();
                interdiff.contexts.push_back(context);
            }
        }
    }
    trace!("DRAINING FIRST PATCH");
    while !patch_1.contexts.is_empty() {
        let context = patch_1.contexts.pop_front().unwrap();
        let flipped = context.flip(context_radius);
        for mut context in flipped.into_iter() {
            context.shift(patch_2_offset);
            patch_1_offset += context.offset();
            interdiff.contexts.push_back(context);
        }
    }
    trace!("DRAINING SECOND PATCH");
    while !patch_2.contexts.is_empty() {
        let context = patch_2.contexts.pop_front().unwrap();
        let reduced = context.reduce(context_radius);
        for mut context in reduced.into_iter() {
            context.shift(-patch_1_offset);
            patch_2_offset += context.offset();
            interdiff.contexts.push_back(context);
        }
    }

    interdiff
}