Skip to content

Instantly share code, notes, and snippets.

@LordGrimmauld
Last active November 14, 2021 13:26
Show Gist options
  • Save LordGrimmauld/e682a8cb05bbf9d11cc82ff84a1b37cc to your computer and use it in GitHub Desktop.
Save LordGrimmauld/e682a8cb05bbf9d11cc82ff84a1b37cc to your computer and use it in GitHub Desktop.
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
def getGitHash = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', '--short', 'HEAD'
standardOutput = stdout
}
return stdout.toString().trim()
}
def hasUnstaged = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'status', '--porcelain'
standardOutput = stdout
}
def result = stdout.toString().replaceAll("M gradlew", "").trim()
if (!result.isEmpty())
println("Found stageable results:\n${result}\n")
return !result.isEmpty()
}
def hasCommandInstalled = { cmd ->
if (!DefaultNativePlatform.currentOperatingSystem.isWindows())
return true
def stdout = new ByteArrayOutputStream()
def stderr = new ByteArrayOutputStream()
return exec {
ignoreExitValue = true
standardOutput = stdout
errorOutput = stderr
commandLine "where", cmd
}.getExitValue() == 0 && stdout.toString().trim().contains(cmd)
}
def getGitInfo = { ->
if (!hasCommandInstalled("git")) {
println("No git install found in PATH")
return "NOGIT"
}
def stdout = new ByteArrayOutputStream()
exec {
standardOutput = stdout
commandLine "git", "--version"
}
println(stdout.toString().trim())
return "${getGitHash()}" + (hasUnstaged() ? "-modified" : "")
}
def testing = hasProperty('TEST_GITINFO')
if(testing) {
println(getGitInfo())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment