From 7c0f6f610947b22e902d348ffc7a06ccadb67cb7 Mon Sep 17 00:00:00 2001 From: Robinson Date: Tue, 31 May 2022 00:14:29 +0200 Subject: [PATCH] Added support for getting the most recent git commit hash --- build.gradle.kts | 2 +- src/dorkbox/version/VersionPlugin.kt | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 7b89a37..2582eda 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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" diff --git a/src/dorkbox/version/VersionPlugin.kt b/src/dorkbox/version/VersionPlugin.kt index ad1ee9c..d8e2aa7 100644 --- a/src/dorkbox/version/VersionPlugin.kt +++ b/src/dorkbox/version/VersionPlugin.kt @@ -262,6 +262,21 @@ class VersionPlugin : Plugin { } } + /** + * 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 { 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)