diff --git a/gitoid/src/gitoid.rs b/gitoid/src/gitoid.rs index 6db51d0..5c504a3 100644 --- a/gitoid/src/gitoid.rs +++ b/gitoid/src/gitoid.rs @@ -552,6 +552,23 @@ impl ForEachChunk for R { } } +fn digest_with_normalized_newlines(buf: &[u8], digester: &mut D) +where + D: Digest, +{ + let not_dos_newline = |char1: &u8, char2: &u8| ((*char1, *char2) == (b'\r', b'\n')).not(); + + let chunks = buf.chunk_by(not_dos_newline); + + for chunk in chunks { + if chunk.last() == Some(&b'\r') { + digester.update(&chunk[0..=(chunk.len() - 2)]); + } else { + digester.update(chunk) + } + } +} + #[cfg(feature = "std")] /// Helper function which actually applies the [`GitOid`] construction rules. /// @@ -572,7 +589,8 @@ where O::NAME.as_bytes(), expected_read_length )); - let amount_read = BufReader::new(reader).for_each_chunk(|b| digester.update(b))?; + let amount_read = BufReader::new(reader) + .for_each_chunk(|b| digest_with_normalized_newlines(b, &mut digester))?; let hash = digester.finalize(); Ok((hash, amount_read)) } @@ -599,7 +617,7 @@ where // It's in memory, so we know the exact size up front. let amount_read = reader.len(); - digester.update(reader); + digest_with_normalized_newlines(reader, &mut digester); let hash = digester.finalize(); Ok((hash, amount_read)) } diff --git a/omnibor-cli/tests/snapshots/test__artifact_id_json.snap b/omnibor-cli/tests/snapshots/test__artifact_id_json.snap index 62e96b5..71a074b 100644 --- a/omnibor-cli/tests/snapshots/test__artifact_id_json.snap +++ b/omnibor-cli/tests/snapshots/test__artifact_id_json.snap @@ -13,6 +13,6 @@ info: success: true exit_code: 0 ----- stdout ----- -{"id":"","path":"tests/data/main.c"} +{"id":"gitoid:blob:sha256:93561f4501717b4c4a2f3eb5776f03231d32ec2a1f709a611ad3d8dcf931dc1b","path":"tests/data/main.c"} ----- stderr ----- diff --git a/omnibor-cli/tests/snapshots/test__artifact_id_plain.snap b/omnibor-cli/tests/snapshots/test__artifact_id_plain.snap index ce746ea..14eeadc 100644 --- a/omnibor-cli/tests/snapshots/test__artifact_id_plain.snap +++ b/omnibor-cli/tests/snapshots/test__artifact_id_plain.snap @@ -13,6 +13,6 @@ info: success: true exit_code: 0 ----- stdout ----- -tests/data/main.c => +tests/data/main.c => gitoid:blob:sha256:93561f4501717b4c4a2f3eb5776f03231d32ec2a1f709a611ad3d8dcf931dc1b ----- stderr ----- diff --git a/omnibor-cli/tests/snapshots/test__artifact_id_short.snap b/omnibor-cli/tests/snapshots/test__artifact_id_short.snap index 57421b6..4da9f62 100644 --- a/omnibor-cli/tests/snapshots/test__artifact_id_short.snap +++ b/omnibor-cli/tests/snapshots/test__artifact_id_short.snap @@ -13,6 +13,6 @@ info: success: true exit_code: 0 ----- stdout ----- - +gitoid:blob:sha256:93561f4501717b4c4a2f3eb5776f03231d32ec2a1f709a611ad3d8dcf931dc1b ----- stderr ----- diff --git a/omnibor-cli/tests/test.rs b/omnibor-cli/tests/test.rs index 4903df8..1d130ea 100644 --- a/omnibor-cli/tests/test.rs +++ b/omnibor-cli/tests/test.rs @@ -6,7 +6,6 @@ macro_rules! settings { ($block:expr) => { let mut settings = Settings::clone_current(); settings.add_filter(r#"omnibor(?:\.exe)?"#, "omnibor"); - settings.add_filter(r#"gitoid:blob:sha256:[a-f0-9]{64}"#, ""); settings.bind(|| $block); }; }