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
pub mod diff {
use crate::bstr::ByteSlice;
use crate::object::blob::diff::line::Change;
use std::ops::Range;
pub struct Platform<'old, 'new> {
pub old: crate::Object<'old>,
pub new: crate::Object<'new>,
pub algo: git_diff::blob::Algorithm,
}
pub mod init {
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("Could not find the previous blob or the new blob to diff against")]
FindExisting(#[from] crate::object::find::existing::Error),
#[error("Could not obtain diff algorithm from configuration")]
DiffAlgorithm(#[from] crate::config::diff::algorithm::Error),
}
}
impl<'old, 'new> Platform<'old, 'new> {
pub fn from_ids(
previous_id: &crate::Id<'old>,
new_id: &crate::Id<'new>,
) -> Result<Platform<'old, 'new>, init::Error> {
match previous_id
.object()
.and_then(|old| new_id.object().map(|new| (old, new)))
{
Ok((old, new)) => {
let algo = match new_id.repo.config.diff_algorithm() {
Ok(algo) => algo,
Err(err) => return Err(err.into()),
};
Ok(Platform { old, new, algo })
}
Err(err) => Err(err.into()),
}
}
}
pub mod line {
use crate::bstr::BStr;
pub enum Change<'a, 'data> {
Addition {
lines: &'a [&'data BStr],
},
Deletion {
lines: &'a [&'data BStr],
},
Modification {
lines_before: &'a [&'data BStr],
lines_after: &'a [&'data BStr],
},
}
}
impl<'old, 'new> Platform<'old, 'new> {
pub fn lines<FnH, E>(&self, mut process_hunk: FnH) -> Result<(), E>
where
FnH: FnMut(line::Change<'_, '_>) -> Result<(), E>,
E: std::error::Error,
{
let input = git_diff::blob::intern::InternedInput::new(self.old.data.as_bytes(), self.new.data.as_bytes());
let mut err = None;
let mut lines = Vec::new();
git_diff::blob::diff(self.algo, &input, |before: Range<u32>, after: Range<u32>| {
if err.is_some() {
return;
}
lines.clear();
lines.extend(
input.before[before.start as usize..before.end as usize]
.iter()
.map(|&line| input.interner[line].as_bstr()),
);
let end_of_before = lines.len();
lines.extend(
input.after[after.start as usize..after.end as usize]
.iter()
.map(|&line| input.interner[line].as_bstr()),
);
let hunk_before = &lines[..end_of_before];
let hunk_after = &lines[end_of_before..];
if hunk_after.is_empty() {
err = process_hunk(Change::Deletion { lines: hunk_before }).err();
} else if hunk_before.is_empty() {
err = process_hunk(Change::Addition { lines: hunk_after }).err();
} else {
err = process_hunk(Change::Modification {
lines_before: hunk_before,
lines_after: hunk_after,
})
.err();
}
});
match err {
Some(err) => Err(err),
None => Ok(()),
}
}
pub fn line_counts(&self) -> git_diff::blob::sink::Counter<()> {
let tokens = self.line_tokens();
git_diff::blob::diff(self.algo, &tokens, git_diff::blob::sink::Counter::default())
}
pub fn line_tokens(&self) -> git_diff::blob::intern::InternedInput<&[u8]> {
git_diff::blob::intern::InternedInput::new(self.old.data.as_bytes(), self.new.data.as_bytes())
}
}
}