Added support for getting the most recent git commit hash
This commit is contained in:
parent
ceb7511b1c
commit
7c0f6f6109
|
@ -32,7 +32,7 @@ object Extras {
|
||||||
// set for the project
|
// 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 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 group = "com.dorkbox"
|
||||||
const val version = "2.4.1"
|
const val version = "2.5"
|
||||||
|
|
||||||
// set as project.ext
|
// set as project.ext
|
||||||
const val name = "Version Update"
|
const val name = "Version Update"
|
||||||
|
|
|
@ -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) {
|
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
|
// 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)
|
val git = getGit(project)
|
||||||
|
@ -634,6 +649,15 @@ class VersionPlugin : Plugin<Project> {
|
||||||
return alreadyParsedFiles
|
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 {
|
private fun getGit(project: Project): Git {
|
||||||
try {
|
try {
|
||||||
val gitDir = getRootGitDir(project.projectDir)
|
val gitDir = getRootGitDir(project.projectDir)
|
||||||
|
|
Loading…
Reference in New Issue