-
Notifications
You must be signed in to change notification settings - Fork 7
/
history.rs
280 lines (265 loc) · 9.97 KB
/
history.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use std::{
borrow::Cow,
collections::{BTreeMap, HashMap},
iter::FromIterator,
path::PathBuf,
};
use anyhow::bail;
use cargo_metadata::Package;
use gix::{
bstr::ByteSlice,
head,
prelude::{ObjectIdExt, ReferenceExt},
Reference,
};
use crate::{
commit,
commit::history::{Item, Segment},
git::strip_tag_path,
utils::{component_to_bytes, is_tag_name, is_tag_version, tag_prefix},
Context,
};
pub enum SegmentScope {
/// Stop finding segments after the unreleased/first section was processed.
Unreleased,
/// Obtain all segments, including unreleased and tags
EntireHistory,
}
pub fn collect(repo: &gix::Repository) -> anyhow::Result<Option<commit::History>> {
let mut handle = repo.clone();
handle.object_cache_size(64 * 1024);
let reference = match handle.head()?.peeled()?.kind {
head::Kind::Detached { .. } => bail!("Refusing to operate on a detached head."),
head::Kind::Unborn { .. } => return Ok(None),
head::Kind::Symbolic(r) => r.attach(&handle),
};
let mut items = Vec::new();
let mut data_by_tree_id = HashMap::default();
for commit_id in reference
.id()
.ancestors()
.sorting(gix::traverse::commit::Sorting::ByCommitTimeNewestFirst)
.use_commit_graph(false)
.all()?
{
let commit = commit_id?;
let (message, tree_id, parent_tree_id, commit_time) = {
let (message, tree_id, commit_time, parent_commit_id) = {
let object = commit.object()?;
let commit = object.decode()?;
let parent = commit.parents().next();
(commit.message.to_vec(), commit.tree(), commit.committer.time, parent)
};
(
message,
tree_id,
parent_commit_id.map(|id| id.attach(&handle).object().expect("present").to_commit_ref().tree()),
commit_time,
)
};
let message = match message.to_str() {
Err(_) => {
log::warn!(
"Commit message of {} could not be decoded to UTF-8 - ignored",
commit.id
);
continue;
}
Ok(m) => m,
};
data_by_tree_id.insert(tree_id, handle.find_object(tree_id)?.data.to_owned());
if let Some(tree_id) = parent_tree_id {
data_by_tree_id.insert(tree_id, handle.find_object(tree_id)?.data.to_owned());
}
items.push(commit::history::Item {
id: commit.id,
commit_time,
message: commit::Message::from(message),
tree_id,
parent_tree_id,
});
}
Ok(Some(commit::History {
head: reference.detach(),
items,
data_by_tree_id,
}))
}
/// Return the head reference followed by all tags affecting `crate_name` as per our tag name rules, ordered by ancestry.
pub fn crate_ref_segments<'h>(
package: &Package,
ctx: &crate::Context,
history: &'h commit::History,
scope: SegmentScope,
) -> anyhow::Result<Vec<commit::history::Segment<'h>>> {
let tag_prefix = tag_prefix(package, &ctx.repo);
let mut tags_by_commit = {
let refs = ctx.repo.references()?;
match tag_prefix {
Some(prefix) => BTreeMap::from_iter(
refs.prefixed(PathBuf::from(format!("refs/tags/{prefix}-")))?
.peeled()
.filter_map(|r| r.ok().map(Reference::detach))
.filter(|r| is_tag_name(prefix, strip_tag_path(r.name.as_ref())))
.map(|r| {
let t = r.peeled.expect("already peeled");
(t, r)
}),
),
None => BTreeMap::from_iter(
refs.prefixed("refs/tags")?
.peeled()
.filter_map(|r| r.ok().map(Reference::detach))
.filter(|r| is_tag_version(strip_tag_path(r.name.as_ref())))
.map(|r| {
let t = r.peeled.expect("already peeled");
(t, r)
}),
),
}
};
let mut segments = Vec::new();
let mut segment = commit::history::Segment {
head: history.head.to_owned(),
history: vec![],
};
let dir = ctx.repo_relative_path(package);
let mut filter = dir.map_or_else(
|| {
if ctx.meta.workspace_members.len() == 1 {
Filter::None
} else {
log::info!(
"{}: Tracking top-level crate's changes in multi-crate workspace through 'src/' directory only.",
package.name
);
// TODO: analyse .targets to find actual source directory.
Filter::Fast {
name: Cow::Borrowed(b"src"),
}
}
},
|dir| {
let mut components = dir.components().collect::<Vec<_>>();
match components.len() {
0 => unreachable!("BUG: it's None if empty"),
1 => Filter::Fast {
name: components.pop().map(component_to_bytes).expect("exactly one").into(),
},
_ => Filter::Slow {
components: components.into_iter().map(component_to_bytes).collect(),
},
}
},
);
for item in &history.items {
match tags_by_commit.remove(&item.id) {
None => add_item_if_package_changed(ctx, &mut segment, &mut filter, item, &history.data_by_tree_id)?,
Some(next_ref) => {
match scope {
SegmentScope::EntireHistory => {
segments.push(std::mem::replace(
&mut segment,
commit::history::Segment {
head: next_ref,
history: vec![],
},
));
}
SegmentScope::Unreleased => {
segments.push(segment);
return Ok(segments);
}
}
add_item_if_package_changed(ctx, &mut segment, &mut filter, item, &history.data_by_tree_id)?
}
}
}
segments.push(segment);
if matches!(scope, SegmentScope::EntireHistory) && !tags_by_commit.is_empty() {
log::warn!(
"{}: The following tags were not encountered during commit graph traversal: {}",
package.name,
tags_by_commit
.into_values()
.map(|v| v.name.as_bstr().to_str_lossy().into_owned())
.collect::<Vec<_>>()
.join(", ")
)
}
Ok(segments)
}
enum Filter<'a> {
/// Unconditionally use history items, we always consider them relevant for the package.
None,
/// The package sits directly at the root, allowing us to use our cached trees exclusively, which is the fastest possible change detection.
Fast { name: Cow<'a, [u8]> },
/// The package sits at a deeper level which means we have to read other trees as well while determining its hash.
Slow { components: Vec<&'a [u8]> },
}
fn add_item_if_package_changed<'a>(
ctx: &Context,
segment: &mut Segment<'a>,
filter: &mut Filter<'_>,
item: &'a Item,
data_by_tree_id: &HashMap<gix::ObjectId, Vec<u8>>,
) -> anyhow::Result<()> {
let history = &mut segment.history;
match filter {
Filter::None => history.push(item),
Filter::Fast { name } => {
let current = gix::objs::TreeRefIter::from_bytes(&data_by_tree_id[&item.tree_id])
.filter_map(Result::ok)
.find(|e| e.filename == name.as_ref());
let parent = item.parent_tree_id.and_then(|parent| {
gix::objs::TreeRefIter::from_bytes(&data_by_tree_id[&parent])
.filter_map(Result::ok)
.find(|e| e.filename == name.as_ref())
});
match (current, parent) {
(Some(current), Some(parent)) => {
if current.oid != parent.oid {
history.push(item)
}
}
(Some(current), None) => {
if let Some(prev_item) = item.parent_tree_id.and_then(|parent| {
gix::objs::TreeRefIter::from_bytes(&data_by_tree_id[&parent])
.filter_map(Result::ok)
.find(|e| e.oid == current.oid)
}) {
log::debug!(
"Tracking package named {:?} as {:?}",
name.as_bstr(),
prev_item.filename
);
*name.to_mut() = prev_item.filename.to_owned().into();
}
history.push(item)
}
(None, _) => {}
};
}
Filter::Slow { ref components } => {
let mut repo = ctx.repo.clone();
repo.object_cache_size(1024 * 1024);
let current = gix::Tree::from_data(item.id, data_by_tree_id[&item.tree_id].to_owned(), &ctx.repo)
.peel_to_entry(components.iter().copied())?;
let parent = match item.parent_tree_id {
Some(tree_id) => gix::Tree::from_data(tree_id, data_by_tree_id[&tree_id].to_owned(), &ctx.repo)
.peel_to_entry(components.iter().copied())?,
None => None,
};
match (current, parent) {
(Some(current), Some(parent)) => {
if current.oid() != parent.oid() {
history.push(item)
}
}
(Some(_), None) => history.push(item),
(None, _) => {}
};
}
};
Ok(())
}