Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not buffer entry contents in memory while doing MergeStrategy.deduplicate #520

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main/scala/sbtassembly/Assembly.scala
Original file line number Diff line number Diff line change
Expand Up @@ -699,21 +699,22 @@ object Assembly {

private[sbtassembly] def sha1 = MessageDigest.getInstance("SHA-1")

private[sbtassembly] def sha1Content(b: Array[Byte]): String =
byteArrayInputStreamResource(b) { in =>
private[sbtassembly] def sha1Content(stream: InputStream): String =
{
val messageDigest = sha1
val buffer = new Array[Byte](8192)

@tailrec
def read(): Unit = {
val byteCount = in.read(buffer)
val byteCount = stream.read(buffer)
if (byteCount >= 0) {
messageDigest.update(buffer, 0, byteCount)
read()
}
}

read()
stream.close()
bytesToString(messageDigest.digest())
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/scala/sbtassembly/MergeStrategy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ object MergeStrategy {
* Verifies if all the conflicts have the same content, otherwise error out
*/
val deduplicate: MergeStrategy = MergeStrategy("Deduplicate") { conflicts =>
val conflictContents = conflicts.map(_.stream()).map(Streamable.bytes(_))
val fingerprints = Set() ++ conflictContents.map(sbtassembly.Assembly.sha1Content)
val fingerprints = conflicts.map(dep =>
sbtassembly.Assembly.sha1Content(dep.stream())
).toSet
if (fingerprints.size == 1)
Right(Vector(JarEntry(conflicts.head.target, () => new ByteArrayInputStream(conflictContents.head))))
Right(Vector(JarEntry(conflicts.head.target, conflicts.head.stream)))
else
Left(
s"Deduplicate found different file contents in the following:$newLineIndented${conflicts.mkString(newLineIndented)}"
Expand Down
Loading