History

change!: `write::Options::object_hash` is now implied by the `State` itself.

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`.

Sebastian Thiel committed 4 years ago

Commit 59f67911 parent fa6bcde8 files · +21 −32Browse files

Unified · Split

8 files
  1. git-index/src/access.rs +5 −0
  2. git-index/src/decode/mod.rs +1 −0
  3. git-index/src/file/write.rs +1 −1
  4. git-index/src/init.rs +1 −0
  5. git-index/src/lib.rs +4 −0
  6. git-index/src/write.rs +3 −12
  7. git-index/tests/index/file/init.rs +1 −4
  8. git-index/tests/index/file/write.rs +5 −15
git-index/src/access.rs+5−0
@@ -9,6 +9,11 @@ impl State { · 8 unmodified lines
99 self.version
1010 }
1111
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+
1217 /// Return our entries
1318 pub fn entries(&self) -> &[Entry] {
1419 &self.entries
git-index/src/decode/mod.rs+1−0
@@ -209,6 +209,7 @@ impl State { · 208 unmodified lines
209209
210210 Ok((
211211 State {
212+ object_hash,
212213 timestamp,
213214 version,
214215 entries,
git-index/src/file/write.rs+1−1
@@ -22,7 +22,7 @@ impl File { · 21 unmodified lines
2222 mut out: impl std::io::Write,
2323 options: write::Options,
2424 ) -> 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);
2626 let version = self.state.write_to(&mut hasher, options)?;
2727
2828 let hash = hasher.hash.digest();
git-index/src/init.rs+1−0
@@ -37,6 +37,7 @@ mod from_tree { · 36 unmodified lines
3737 entries.sort_by(|a, b| Entry::cmp_filepaths(a.path_in(&path_backing), b.path_in(&path_backing)));
3838
3939 Ok(State {
40+ object_hash: tree.kind(),
4041 timestamp: filetime::FileTime::now(),
4142 version: Version::V2,
4243 entries,
git-index/src/lib.rs+4−0
@@ -81,6 +81,10 @@ pub type PathStorageRef = [u8]; · 80 unmodified lines
8181 /// We treat index and its state synonymous.
8282 #[derive(Clone)]
8383 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,
8488 /// The time at which the state was created, indicating its freshness compared to other files on disk.
8589 ///
8690 /// Note that on platforms that only have a precisions of a second for this time, we will treat all entries with the
git-index/src/write.rs+3−12
@@ -46,24 +46,15 @@ impl Extensions { · 45 unmodified lines
4646 /// The options for use when [writing an index][State::write_to()].
4747 ///
4848 /// 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)]
5050 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−
5651 /// Configures which extensions to write
5752 pub extensions: Extensions,
5853 }
5954
6055 impl State {
6156 /// 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> {
6758 let version = self.detect_required_version();
6859
6960 let mut write = CountBytes::new(out);
@@ -83,7 +74,7 @@ impl State { · 13 unmodified lines
8374 .is_some()
8475 && !extension_toc.is_empty()
8576 {
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)?
8778 }
8879
8980 Ok(version)
git-index/tests/index/file/init.rs+1−4
@@ -23,10 +23,7 @@ mod from_state { · 22 unmodified lines
2323 assert!(index.checksum().is_none());
2424 assert_eq!(index.path(), index_path);
2525
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())?;
3027 assert!(index.checksum().is_some(), "checksum is adjusted after writing");
3128 assert!(index.path().is_file());
3229 assert_eq!(index.version(), expected_version,);
git-index/tests/index/file/write.rs+5−15
@@ -11,8 +11,8 @@ fn roundtrips() -> crate::Result { · 10 unmodified lines
1111 (Loose("extended-flags"), all_ext_but_eoie()),
1212 (Loose("conflicting-file"), all_ext_but_eoie()),
1313 (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()),
1616 (Generated("v2_more_files"), all_ext_but_eoie()),
1717 (Generated("v2_all_file_kinds"), all_ext_but_eoie()),
1818 ];
@@ -44,10 +44,7 @@ fn roundtrips() -> crate::Result { · 25 unmodified lines
4444 #[test]
4545 fn state_comparisons_with_various_extension_configurations() {
4646 fn options_with(extensions: write::Extensions) -> Options {
47− Options {
48− hash_kind: git_hash::Kind::Sha1,
49− extensions,
50− }
47+ Options { extensions }
5148 }
5249
5350 for fixture in [
@@ -98,7 +95,7 @@ fn extended_flags_automatically_upgrade_the_version_to_avoid_data_loss() -> crat · 44 unmodified lines
9895 expected.entries_mut()[0].flags.insert(entry::Flags::EXTENDED);
9996
10097 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())?;
10299 assert_eq!(actual_version, Version::V3, "extended flags need V3");
103100
104101 Ok(())
@@ -158,13 +155,6 @@ fn all_ext_but_eoie() -> Options { · 53 unmodified lines
158155 end_of_index_entry: false,
159156 tree_cache: true,
160157 },
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()
169159 }
170160 }