The `State`, once initialized, knows the kind of object hash it uses and there is no need to specify it again. This affects some method signatures which now work without `object_hash`.
Unified · Split
| @@ -9,6 +9,11 @@ impl State { · 8 unmodified lines | ||
| 9 | 9 | self.version |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | + /// Return the kind of hashes used in this instance. | |
| 13 | + pub fn object_hash(&self) -> git_hash::Kind { | |
| 14 | + self.object_hash | |
| 15 | + } | |
| 16 | + | |
| 12 | 17 | /// Return our entries |
| 13 | 18 | pub fn entries(&self) -> &[Entry] { |
| 14 | 19 | &self.entries |
| @@ -209,6 +209,7 @@ impl State { · 208 unmodified lines | ||
| 209 | 209 | |
| 210 | 210 | Ok(( |
| 211 | 211 | State { |
| 212 | + object_hash, | |
| 212 | 213 | timestamp, |
| 213 | 214 | version, |
| 214 | 215 | entries, |
| @@ -22,7 +22,7 @@ impl File { · 21 unmodified lines | ||
| 22 | 22 | mut out: impl std::io::Write, |
| 23 | 23 | options: write::Options, |
| 24 | 24 | ) -> std::io::Result<(Version, git_hash::ObjectId)> { |
| 25 | − let mut hasher = hash::Write::new(&mut out, options.hash_kind); | |
| 25 | + let mut hasher = hash::Write::new(&mut out, self.state.object_hash); | |
| 26 | 26 | let version = self.state.write_to(&mut hasher, options)?; |
| 27 | 27 | |
| 28 | 28 | let hash = hasher.hash.digest(); |
| @@ -37,6 +37,7 @@ mod from_tree { · 36 unmodified lines | ||
| 37 | 37 | entries.sort_by(|a, b| Entry::cmp_filepaths(a.path_in(&path_backing), b.path_in(&path_backing))); |
| 38 | 38 | |
| 39 | 39 | Ok(State { |
| 40 | + object_hash: tree.kind(), | |
| 40 | 41 | timestamp: filetime::FileTime::now(), |
| 41 | 42 | version: Version::V2, |
| 42 | 43 | entries, |
| @@ -81,6 +81,10 @@ pub type PathStorageRef = [u8]; · 80 unmodified lines | ||
| 81 | 81 | /// We treat index and its state synonymous. |
| 82 | 82 | #[derive(Clone)] |
| 83 | 83 | pub struct State { |
| 84 | + /// The kind of object hash used when storing the underlying file. | |
| 85 | + /// | |
| 86 | + /// Empty states for example won't have a single object id, so deduction of the hash used isn't always possible. | |
| 87 | + object_hash: git_hash::Kind, | |
| 84 | 88 | /// The time at which the state was created, indicating its freshness compared to other files on disk. |
| 85 | 89 | /// |
| 86 | 90 | /// Note that on platforms that only have a precisions of a second for this time, we will treat all entries with the |
| @@ -46,24 +46,15 @@ impl Extensions { · 45 unmodified lines | ||
| 46 | 46 | /// The options for use when [writing an index][State::write_to()]. |
| 47 | 47 | /// |
| 48 | 48 | /// Note that default options write either index V2 or V3 depending on the content of the entries. |
| 49 | −#[derive(Debug, Clone, Copy)] | |
| 49 | +#[derive(Debug, Default, Clone, Copy)] | |
| 50 | 50 | pub struct Options { |
| 51 | − /// The hash kind to use when writing the index file. | |
| 52 | − /// | |
| 53 | − /// It is not always possible to infer the hash kind when reading an index, so this is required. | |
| 54 | − pub hash_kind: git_hash::Kind, | |
| 55 | − | |
| 56 | 51 | /// Configures which extensions to write |
| 57 | 52 | pub extensions: Extensions, |
| 58 | 53 | } |
| 59 | 54 | |
| 60 | 55 | impl State { |
| 61 | 56 | /// Serialize this instance to `out` with [`options`][Options]. |
| 62 | − pub fn write_to( | |
| 63 | − &self, | |
| 64 | − out: impl std::io::Write, | |
| 65 | − Options { hash_kind, extensions }: Options, | |
| 66 | − ) -> std::io::Result<Version> { | |
| 57 | + pub fn write_to(&self, out: impl std::io::Write, Options { extensions }: Options) -> std::io::Result<Version> { | |
| 67 | 58 | let version = self.detect_required_version(); |
| 68 | 59 | |
| 69 | 60 | let mut write = CountBytes::new(out); |
| @@ -83,7 +74,7 @@ impl State { · 13 unmodified lines | ||
| 83 | 74 | .is_some() |
| 84 | 75 | && !extension_toc.is_empty() |
| 85 | 76 | { |
| 86 | − extension::end_of_index_entry::write_to(out, hash_kind, offset_to_extensions, extension_toc)? | |
| 77 | + extension::end_of_index_entry::write_to(out, self.object_hash, offset_to_extensions, extension_toc)? | |
| 87 | 78 | } |
| 88 | 79 | |
| 89 | 80 | Ok(version) |
| @@ -23,10 +23,7 @@ mod from_state { · 22 unmodified lines | ||
| 23 | 23 | assert!(index.checksum().is_none()); |
| 24 | 24 | assert_eq!(index.path(), index_path); |
| 25 | 25 | |
| 26 | − index.write(git_index::write::Options { | |
| 27 | − hash_kind: git_hash::Kind::Sha1, | |
| 28 | − extensions: Default::default(), | |
| 29 | − })?; | |
| 26 | + index.write(git_index::write::Options::default())?; | |
| 30 | 27 | assert!(index.checksum().is_some(), "checksum is adjusted after writing"); |
| 31 | 28 | assert!(index.path().is_file()); |
| 32 | 29 | assert_eq!(index.version(), expected_version,); |
| @@ -11,8 +11,8 @@ fn roundtrips() -> crate::Result { · 10 unmodified lines | ||
| 11 | 11 | (Loose("extended-flags"), all_ext_but_eoie()), |
| 12 | 12 | (Loose("conflicting-file"), all_ext_but_eoie()), |
| 13 | 13 | (Loose("very-long-path"), all_ext_but_eoie()), |
| 14 | − (Generated("v2"), sha1_options()), | |
| 15 | − (Generated("V2_empty"), sha1_options()), | |
| 14 | + (Generated("v2"), Default::default()), | |
| 15 | + (Generated("V2_empty"), Default::default()), | |
| 16 | 16 | (Generated("v2_more_files"), all_ext_but_eoie()), |
| 17 | 17 | (Generated("v2_all_file_kinds"), all_ext_but_eoie()), |
| 18 | 18 | ]; |
| @@ -44,10 +44,7 @@ fn roundtrips() -> crate::Result { · 25 unmodified lines | ||
| 44 | 44 | #[test] |
| 45 | 45 | fn state_comparisons_with_various_extension_configurations() { |
| 46 | 46 | fn options_with(extensions: write::Extensions) -> Options { |
| 47 | − Options { | |
| 48 | − hash_kind: git_hash::Kind::Sha1, | |
| 49 | − extensions, | |
| 50 | − } | |
| 47 | + Options { extensions } | |
| 51 | 48 | } |
| 52 | 49 | |
| 53 | 50 | for fixture in [ |
| @@ -98,7 +95,7 @@ fn extended_flags_automatically_upgrade_the_version_to_avoid_data_loss() -> crat · 44 unmodified lines | ||
| 98 | 95 | expected.entries_mut()[0].flags.insert(entry::Flags::EXTENDED); |
| 99 | 96 | |
| 100 | 97 | let mut buf = Vec::new(); |
| 101 | − let (actual_version, _digest) = expected.write_to(&mut buf, sha1_options())?; | |
| 98 | + let (actual_version, _digest) = expected.write_to(&mut buf, Default::default())?; | |
| 102 | 99 | assert_eq!(actual_version, Version::V3, "extended flags need V3"); |
| 103 | 100 | |
| 104 | 101 | Ok(()) |
| @@ -158,13 +155,6 @@ fn all_ext_but_eoie() -> Options { · 53 unmodified lines | ||
| 158 | 155 | end_of_index_entry: false, |
| 159 | 156 | tree_cache: true, |
| 160 | 157 | }, |
| 161 | − ..sha1_options() | |
| 162 | − } | |
| 163 | −} | |
| 164 | − | |
| 165 | −fn sha1_options() -> git_index::write::Options { | |
| 166 | − git_index::write::Options { | |
| 167 | − hash_kind: git_hash::Kind::Sha1, | |
| 168 | − extensions: Default::default(), | |
| 158 | + ..Default::default() | |
| 169 | 159 | } |
| 170 | 160 | } |