Skip to content

Commit 6e319fa

Browse files
committed
[Build] Normalize EOL of the generated files when computing checksums to avoid false positives.
1 parent 2292266 commit 6e319fa

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

build.gradle

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,8 @@ wrapper {
759759
class ChecksumTask extends DefaultTask
760760
{
761761
private static final MessageDigest SHA_256 = MessageDigest.getInstance("SHA-256");
762+
private static final byte CR = (byte) ('\r' as char)
763+
private static final byte LF = (byte) ('\n' as char)
762764

763765
@InputDirectory
764766
File inputDirectory
@@ -773,7 +775,9 @@ class ChecksumTask extends DefaultTask
773775
final File[] files = inputDirectory.listFiles();
774776
for (final File f : files)
775777
{
776-
SHA_256.update(Files.readAllBytes(f.toPath()))
778+
final byte[] raw = Files.readAllBytes(f.toPath())
779+
final byte[] normalized = normalizeEOL(raw);
780+
SHA_256.update(normalized)
777781
}
778782

779783
final byte[] hash = SHA_256.digest()
@@ -785,4 +789,19 @@ class ChecksumTask extends DefaultTask
785789
StandardOpenOption.WRITE,
786790
StandardOpenOption.TRUNCATE_EXISTING)
787791
}
792+
793+
private byte[] normalizeEOL(final byte[] raw)
794+
{
795+
final byte[] result = new byte[raw.length]
796+
int i = 0
797+
for (int j = 0, size = raw.length; j < size; j++)
798+
{
799+
if (CR == raw[j] && (j == size - 1 || LF == raw[j + 1]))
800+
{
801+
continue;
802+
}
803+
result[i++] = raw[j]
804+
}
805+
return raw.length == i ? result : Arrays.copyOf(result, i);
806+
}
788807
}

0 commit comments

Comments
 (0)