Skip to content

Instantly share code, notes, and snippets.

@PaulWoitaschek
Created May 12, 2023 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulWoitaschek/d4686579441a01702e1c481a46f327d2 to your computer and use it in GitHub Desktop.
Save PaulWoitaschek/d4686579441a01702e1c481a46f327d2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env kotlin
@file:DependsOn("com.github.ajalt.clikt:clikt-jvm:3.5.2")
@file:OptIn(ExperimentalTime::class)
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import java.io.File
import java.security.MessageDigest
import kotlin.time.ExperimentalTime
import kotlin.time.measureTime
class App : CliktCommand() {
private val bad by option().required()
private val good by option().required()
override fun run() {
val commitsBetween = executeCommand("git", "rev-list", "--ancestry-path", "$good..$bad")
.filter(String::isNotEmpty)
.reversed()
val output = File("apks")
.also { it.mkdir() }
commitsBetween
.forEachIndexed { index, sha ->
println("checking out $index:$sha")
val apk = File("app/build/outputs/apk/debug/app-debug.apk")
apk.delete()
measureTime {
executeCommand("git", "checkout", sha)
executeCommand("./gradlew", "app:assembleDebug")
apk.copyTo(File(output, "$index-$sha.apk"))
}.also { println("took $it") }
}
deleteDupes(output)
}
private fun deleteDupes(output: File) {
output.listFiles()!!.toList()
.groupBy {
calculateFileHash(it)
}
.forEach { (_, files) ->
if (files.size > 1) {
files.drop(1).forEach {
it.delete()
}
}
}
}
private fun calculateFileHash(file: File): String {
val md = MessageDigest.getInstance("SHA-256")
val digest = md.digest(file.readBytes())
return digest.joinToString("") { "%02x".format(it) }
}
private fun executeCommand(vararg command: String): List<String> {
return Runtime
.getRuntime()
.exec(command)
.inputStream
.bufferedReader()
.use { it.readLines() }
}
}
App().main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment