Skip to content

Commit

Permalink
do not buffer entry contents in memory while doing MergeStrategy.dedu…
Browse files Browse the repository at this point in the history
…plicate
  • Loading branch information
shuttie committed Mar 11, 2024
1 parent f0014e7 commit 181a648
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
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

0 comments on commit 181a648

Please sign in to comment.