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
9 self.version9 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 /// Return our entries17 /// Return our entries
13 pub fn entries(&self) -> &[Entry] {18 pub fn entries(&self) -> &[Entry] {
14 &self.entries19 &self.entries
git-index/src/decode/mod.rs+1−0
@@ -209,6 +209,7 @@ impl State { · 208 unmodified lines
209 209
210 Ok((210 Ok((
211 State {211 State {
212 object_hash,
212 timestamp,213 timestamp,
213 version,214 version,
214 entries,215 entries,
git-index/src/file/write.rs+1−1
@@ -22,7 +22,7 @@ impl File { · 21 unmodified lines
22 mut out: impl std::io::Write,22 mut out: impl std::io::Write,
23 options: write::Options,23 options: write::Options,
24 ) -> std::io::Result<(Version, git_hash::ObjectId)> {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 let version = self.state.write_to(&mut hasher, options)?;26 let version = self.state.write_to(&mut hasher, options)?;
27 27
28 let hash = hasher.hash.digest();28 let hash = hasher.hash.digest();
git-index/src/init.rs+1−0
@@ -37,6 +37,7 @@ mod from_tree { · 36 unmodified lines
37 entries.sort_by(|a, b| Entry::cmp_filepaths(a.path_in(&path_backing), b.path_in(&path_backing)));37 entries.sort_by(|a, b| Entry::cmp_filepaths(a.path_in(&path_backing), b.path_in(&path_backing)));
38 38
39 Ok(State {39 Ok(State {
40 object_hash: tree.kind(),
40 timestamp: filetime::FileTime::now(),41 timestamp: filetime::FileTime::now(),
41 version: Version::V2,42 version: Version::V2,
42 entries,43 entries,
git-index/src/lib.rs+4−0
@@ -81,6 +81,10 @@ pub type PathStorageRef = [u8]; · 80 unmodified lines
81/// We treat index and its state synonymous.81/// We treat index and its state synonymous.
82#[derive(Clone)]82#[derive(Clone)]
83pub struct State {83pub 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 /// The time at which the state was created, indicating its freshness compared to other files on disk.88 /// The time at which the state was created, indicating its freshness compared to other files on disk.
85 ///89 ///
86 /// Note that on platforms that only have a precisions of a second for this time, we will treat all entries with the90 /// 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
46/// The options for use when [writing an index][State::write_to()].46/// The options for use when [writing an index][State::write_to()].
47///47///
48/// Note that default options write either index V2 or V3 depending on the content of the entries.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)]
50pub struct Options {50pub 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 /// Configures which extensions to write51 /// Configures which extensions to write
57 pub extensions: Extensions,52 pub extensions: Extensions,
58}53}
59 54
60impl State {55impl State {
61 /// Serialize this instance to `out` with [`options`][Options].56 /// Serialize this instance to `out` with [`options`][Options].
62 pub fn write_to(57 pub fn write_to(&self, out: impl std::io::Write, Options { extensions }: Options) -> std::io::Result<Version> {
63 &self,
64 out: impl std::io::Write,
65 Options { hash_kind, extensions }: Options,
66 ) -> std::io::Result<Version> {
67 let version = self.detect_required_version();58 let version = self.detect_required_version();
68 59
69 let mut write = CountBytes::new(out);60 let mut write = CountBytes::new(out);
@@ -83,7 +74,7 @@ impl State { · 13 unmodified lines
83 .is_some()74 .is_some()
84 && !extension_toc.is_empty()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 Ok(version)80 Ok(version)
git-index/tests/index/file/init.rs+1−4
@@ -23,10 +23,7 @@ mod from_state { · 22 unmodified lines
23 assert!(index.checksum().is_none());23 assert!(index.checksum().is_none());
24 assert_eq!(index.path(), index_path);24 assert_eq!(index.path(), index_path);
25 25
26 index.write(git_index::write::Options {26 index.write(git_index::write::Options::default())?;
27 hash_kind: git_hash::Kind::Sha1,
28 extensions: Default::default(),
29 })?;
30 assert!(index.checksum().is_some(), "checksum is adjusted after writing");27 assert!(index.checksum().is_some(), "checksum is adjusted after writing");
31 assert!(index.path().is_file());28 assert!(index.path().is_file());
32 assert_eq!(index.version(), expected_version,);29 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
11 (Loose("extended-flags"), all_ext_but_eoie()),11 (Loose("extended-flags"), all_ext_but_eoie()),
12 (Loose("conflicting-file"), all_ext_but_eoie()),12 (Loose("conflicting-file"), all_ext_but_eoie()),
13 (Loose("very-long-path"), all_ext_but_eoie()),13 (Loose("very-long-path"), all_ext_but_eoie()),
14 (Generated("v2"), sha1_options()),14 (Generated("v2"), Default::default()),
15 (Generated("V2_empty"), sha1_options()),15 (Generated("V2_empty"), Default::default()),
16 (Generated("v2_more_files"), all_ext_but_eoie()),16 (Generated("v2_more_files"), all_ext_but_eoie()),
17 (Generated("v2_all_file_kinds"), all_ext_but_eoie()),17 (Generated("v2_all_file_kinds"), all_ext_but_eoie()),
18 ];18 ];
@@ -44,10 +44,7 @@ fn roundtrips() -> crate::Result { · 25 unmodified lines
44#[test]44#[test]
45fn state_comparisons_with_various_extension_configurations() {45fn state_comparisons_with_various_extension_configurations() {
46 fn options_with(extensions: write::Extensions) -> Options {46 fn options_with(extensions: write::Extensions) -> Options {
47 Options {47 Options { extensions }
48 hash_kind: git_hash::Kind::Sha1,
49 extensions,
50 }
51 }48 }
52 49
53 for fixture in [50 for fixture in [
@@ -98,7 +95,7 @@ fn extended_flags_automatically_upgrade_the_version_to_avoid_data_loss() -> crat · 44 unmodified lines
98 expected.entries_mut()[0].flags.insert(entry::Flags::EXTENDED);95 expected.entries_mut()[0].flags.insert(entry::Flags::EXTENDED);
99 96
100 let mut buf = Vec::new();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 assert_eq!(actual_version, Version::V3, "extended flags need V3");99 assert_eq!(actual_version, Version::V3, "extended flags need V3");
103 100
104 Ok(())101 Ok(())
@@ -158,13 +155,6 @@ fn all_ext_but_eoie() -> Options { · 53 unmodified lines
158 end_of_index_entry: false,155 end_of_index_entry: false,
159 tree_cache: true,156 tree_cache: true,
160 },157 },
161 ..sha1_options()158 ..Default::default()
162 }
163}
164
165fn sha1_options() -> git_index::write::Options {
166 git_index::write::Options {
167 hash_kind: git_hash::Kind::Sha1,
168 extensions: Default::default(),
169 }159 }
170}160}