Added support for getting the most recent git commit hash

master
Robinson 2022-05-31 00:14:29 +02:00
parent ceb7511b1c
commit 7c0f6f6109
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C
2 changed files with 25 additions and 1 deletions

View File

@ -32,7 +32,7 @@ object Extras {
// set for the project
const val description = "Gradle Plugin to update version information and git tags within the Gradle project and java/kotlin files"
const val group = "com.dorkbox"
const val version = "2.4.1"
const val version = "2.5"
// set as project.ext
const val name = "Version Update"

View File

@ -262,6 +262,21 @@ class VersionPlugin : Plugin<Project> {
}
}
/**
* Gets the most recent commit hash in the specified repository.
*/
fun gitCommitHash(directory: File, length: Int = 7) : String {
val latestCommit = getGit(directory).log().setMaxCount(1).call().iterator().next()
val latestCommitHash = latestCommit.name
return if (latestCommitHash?.isNotEmpty() == true) {
val maxLength = length.coerceAtMost(latestCommitHash.length)
latestCommitHash.substring(0, maxLength)
} else {
"NO_HASH"
}
}
fun createTag(project: Project, newVersion: Version) {
// make sure all code is committed (no un-committed files and no staged files). Throw error and exit if there is
val git = getGit(project)
@ -634,6 +649,15 @@ class VersionPlugin : Plugin<Project> {
return alreadyParsedFiles
}
private fun getGit(directory: File): Git {
try {
val gitDir = getRootGitDir(directory)
return Git.wrap(FileRepository(gitDir))
} catch (e: IOException) {
throw RuntimeException(e)
}
}
private fun getGit(project: Project): Git {
try {
val gitDir = getRootGitDir(project.projectDir)