They must remain sorted or else the world will stop spinning.
Showing the changes to git-object/src/mutable (1 of 3 files). Show all
Unified · Split
| @@ -24,7 +24,7 @@ impl From<Error> for io::Error { · 23 unmodified lines | |||
| 24 | #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)] | 24 | #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)] |
| 25 | #[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] | 25 | #[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] |
| 26 | pub struct Tree { | 26 | 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]. |
| 28 | pub entries: Vec<Entry>, | 28 | pub entries: Vec<Entry>, |
| 29 | } | 29 | } |
| 30 | 30 | ||
| @@ -59,6 +59,15 @@ impl Mode { · 28 unmodified lines | |||
| 59 | impl Tree { | 59 | impl Tree { |
| 60 | /// Serialize this tree to `out` in the git internal format. | 60 | /// Serialize this tree to `out` in the git internal format. |
| 61 | pub fn write_to(&self, mut out: impl io::Write) -> io::Result<()> { | 61 | 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 | ); | ||
| 62 | for Entry { mode, filename, oid } in &self.entries { | 71 | for Entry { mode, filename, oid } in &self.entries { |
| 63 | out.write_all(mode.as_bytes())?; | 72 | out.write_all(mode.as_bytes())?; |
| 64 | out.write_all(SPACE)?; | 73 | out.write_all(SPACE)?; |