Skip to content

Instantly share code, notes, and snippets.

@eefret
Created June 13, 2017 06:37
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 eefret/23eaa35588743c7b02530bedd3518cc0 to your computer and use it in GitHub Desktop.
Save eefret/23eaa35588743c7b02530bedd3518cc0 to your computer and use it in GitHub Desktop.
This automatically sets your version number based on your git commit count.
defaultConfig {
# Add 10000 to move past old SVN revisions.
versionCode gitCommitCount() + 10000 ?: 0
}
def gitCommitCount() {
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-list', '--count', 'HEAD'
standardOutput = stdout
}
def commitCount = stdout.toString().trim().toInteger()
return commitCount
}
catch (ignored) {
return 0;
}
}
def gitRevision() {
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-list', 'HEAD', '-n', '1'
standardOutput = stdout
}
def commitRevision = stdout.toString().trim()
return commitRevision
}
catch (ignored) {
return "(unknown revision)";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment