Skip to content

Commit d94f84c

Browse files
committed
More explicit expectations towards entries in mutable Trees
They must remain sorted or else the world will stop spinning.
1 parent f19ea33 commit d94f84c

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

git-diff/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ doctest = false
1313
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1414

1515
[dependencies]
16+
git-object = { version = "0.7", path = "../git-object" }

git-object/src/mutable/tree.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl From<Error> for io::Error {
2424
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
2525
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
2626
pub struct Tree {
27-
/// The directories and files contained in this tree.
27+
/// The directories and files contained in this tree. They must be and remain sorted by [`filename`][Entry::filename].
2828
pub entries: Vec<Entry>,
2929
}
3030

@@ -59,6 +59,15 @@ impl Mode {
5959
impl Tree {
6060
/// Serialize this tree to `out` in the git internal format.
6161
pub fn write_to(&self, mut out: impl io::Write) -> io::Result<()> {
62+
debug_assert_eq!(
63+
&{
64+
let mut entries_sorted = self.entries.clone();
65+
entries_sorted.sort_by(|lhs, rhs| lhs.filename.cmp(&rhs.filename));
66+
entries_sorted
67+
},
68+
&self.entries,
69+
"entries for serialization must be sorted by filename"
70+
);
6271
for Entry { mode, filename, oid } in &self.entries {
6372
out.write_all(mode.as_bytes())?;
6473
out.write_all(SPACE)?;

0 commit comments

Comments
 (0)