Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ef5378

Browse files
committed
generalize extension writing so that writing more will be easier
1 parent 18b722e commit 8ef5378

2 files changed

Lines changed: 29 additions & 15 deletions

File tree

git-index/src/extension/end_of_index_entry/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn write_to(
1111
out: &mut impl std::io::Write,
1212
hash_kind: git_hash::Kind,
1313
offset_to_extensions: u32,
14-
prior_extensions: &[(Signature, u32)],
14+
prior_extensions: impl Iterator<Item = (Signature, u32)>,
1515
) -> Result<(), std::io::Error> {
1616
out.write_all(&SIGNATURE)?;
1717
let extension_size: u32 = 4 + hash_kind.len_in_bytes() as u32;
@@ -21,7 +21,7 @@ pub fn write_to(
2121

2222
let mut hasher = git_features::hash::hasher(hash_kind);
2323
for (signature, size) in prior_extensions {
24-
hasher.update(signature);
24+
hasher.update(&signature);
2525
hasher.update(&size.to_be_bytes());
2626
}
2727
out.write_all(&hasher.digest())?;

git-index/src/write.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,35 @@ impl State {
6363
let offset_to_entries = header(&mut write, version, num_entries)?;
6464
let offset_to_extensions = entries(&mut write, self, offset_to_entries)?;
6565

66-
let mut extensions = Vec::with_capacity(5);
67-
if let Some(offset_past_tree_ext) = tree_cache_extension
68-
.then(|| self.tree())
69-
.flatten()
70-
.map(|tree| tree.write_to(&mut write).map(|_| write.count))
71-
.transpose()?
72-
{
73-
extensions.push((
74-
extension::tree::SIGNATURE,
75-
offset_past_tree_ext - offset_to_extensions - (extension::MIN_SIZE as u32),
76-
));
77-
}
66+
let extensions = {
67+
let extensions: &[&dyn Fn(&mut dyn std::io::Write) -> Option<std::io::Result<extension::Signature>>] =
68+
&[&|write| {
69+
tree_cache_extension
70+
.then(|| self.tree())
71+
.flatten()
72+
.map(|tree| tree.write_to(write).map(|_| extension::tree::SIGNATURE))
73+
}];
74+
75+
let mut offset_to_previous_ext = offset_to_extensions;
76+
let mut out = Vec::with_capacity(5);
77+
for write_ext in extensions {
78+
if let Some(signature) = write_ext(&mut write).transpose()? {
79+
let offset_past_ext = write.count;
80+
let ext_size = offset_past_ext - offset_to_previous_ext - (extension::MIN_SIZE as u32);
81+
offset_to_previous_ext = offset_past_ext;
82+
out.push((signature, ext_size));
83+
}
84+
}
85+
out
86+
};
7887

7988
if num_entries > 0 && end_of_index_entry_extension && !extensions.is_empty() {
80-
extension::end_of_index_entry::write_to(write.inner, hash_kind, offset_to_extensions, &extensions)?
89+
extension::end_of_index_entry::write_to(
90+
write.inner,
91+
hash_kind,
92+
offset_to_extensions,
93+
extensions.into_iter(),
94+
)?
8195
}
8296

8397
Ok(())

0 commit comments

Comments
 (0)