Skip to content

Instantly share code, notes, and snippets.

@kiwiandroiddev
Created April 22, 2016 03:37
Show Gist options
  • Select an option

  • Save kiwiandroiddev/6083e33945744cf4f69473bb16e9517c to your computer and use it in GitHub Desktop.

Select an option

Save kiwiandroiddev/6083e33945744cf4f69473bb16e9517c to your computer and use it in GitHub Desktop.
Git interface functions for gradle build scripts (e.g. for Android). To use add this to your build script: apply from: 'git.gradle'
def getGitCommit() {
return 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim()
}
def getGitTag() {
def p = Runtime.getRuntime().exec("git describe HEAD --abbrev=0")
if (p.waitFor() != 0) {
return "none" // no tag
}
return p.getInputStream().readLines().get(0).toString().trim()
}
def getGitBranchCommitNumber() {
def p = Runtime.getRuntime().exec("git rev-list HEAD --count")
if (p.waitFor() != 0) {
return 0 // no git revisions
}
return p.getInputStream().readLines().get(0).toInteger()
}
// Export functions by turning them into closures
ext {
getGitTag = this.&getGitTag
getGitCommit = this.&getGitCommit
getGitBranchCommitNumber = this.&getGitBranchCommitNumber
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment