Skip to content

Commit

Permalink
feat: Implement newline normalization.
Browse files Browse the repository at this point in the history
This should ensure consistent Artifact IDs whether on Windows or
Unix systems.

Signed-off-by: Andrew Lilley Brinker <[email protected]>
  • Loading branch information
alilleybrinker committed Nov 19, 2024
1 parent b9c2ad0 commit 11bd89b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
22 changes: 20 additions & 2 deletions gitoid/src/gitoid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,23 @@ impl<R: BufRead> ForEachChunk for R {
}
}

fn digest_with_normalized_newlines<D>(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.
///
Expand All @@ -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))
}
Expand All @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion omnibor-cli/tests/snapshots/test__artifact_id_json.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ info:
success: true
exit_code: 0
----- stdout -----
{"id":"<GITOID>","path":"tests/data/main.c"}
{"id":"gitoid:blob:sha256:93561f4501717b4c4a2f3eb5776f03231d32ec2a1f709a611ad3d8dcf931dc1b","path":"tests/data/main.c"}

----- stderr -----
2 changes: 1 addition & 1 deletion omnibor-cli/tests/snapshots/test__artifact_id_plain.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ info:
success: true
exit_code: 0
----- stdout -----
tests/data/main.c => <GITOID>
tests/data/main.c => gitoid:blob:sha256:93561f4501717b4c4a2f3eb5776f03231d32ec2a1f709a611ad3d8dcf931dc1b

----- stderr -----
2 changes: 1 addition & 1 deletion omnibor-cli/tests/snapshots/test__artifact_id_short.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ info:
success: true
exit_code: 0
----- stdout -----
<GITOID>
gitoid:blob:sha256:93561f4501717b4c4a2f3eb5776f03231d32ec2a1f709a611ad3d8dcf931dc1b

----- stderr -----
1 change: 0 additions & 1 deletion omnibor-cli/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"#, "<GITOID>");
settings.bind(|| $block);
};
}
Expand Down

0 comments on commit 11bd89b

Please sign in to comment.