From ab364712fe92d90243d3d47114c8dd488ba96ab1 Mon Sep 17 00:00:00 2001 From: mcclure Date: Thu, 16 Feb 2023 14:20:52 -0500 Subject: [PATCH] Previous attempt to fix git sha on non-git build broke git build. (#3322) This uses unusual code provided by Mikhail Lopatkin of Gradle Inc: https://github.com/gradle/gradle/issues/23914#issuecomment-1431909019 It wraps the git sha function in a "value source". This allows it to interact correctly with configuration caching. Because the code is longer than before, it is now broken out into its own file getGitSha.gradle. --- app/build.gradle | 14 ++------------ app/getGitSha.gradle | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 app/getGitSha.gradle diff --git a/app/build.gradle b/app/build.gradle index 283a8374..ab77fe0a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -5,19 +5,9 @@ plugins { alias(libs.plugins.kotlin.parcelize) } -// For constructing gitSha only -def getGitSha = { - try { // Try-catch is necessary for build to work on non-git distributions - providers.exec { - commandLine 'git', 'rev-parse', 'HEAD' - executionResult.rethrowFailure() // Without this, sometimes it just stops immediately instead of throwing - }.standardOutput.asText.get().trim() - } catch (Exception e) { - "unknown" - } -} +apply from: 'getGitSha.gradle' -final def gitSha = getGitSha() +final def gitSha = ext.getGitSha() // The app name final def APP_NAME = "Tusky" diff --git a/app/getGitSha.gradle b/app/getGitSha.gradle new file mode 100644 index 00000000..53315b2b --- /dev/null +++ b/app/getGitSha.gradle @@ -0,0 +1,27 @@ +import org.gradle.api.provider.ValueSourceParameters +import javax.inject.Inject + +// Must wrap this in a ValueSource in order to get well-defined fail behavior without confusing Gradle on repeat builds. +abstract class GitShaValueSource implements ValueSource { + @Inject abstract ExecOperations getExecOperations() + + @Override String obtain() { + try { + def output = new ByteArrayOutputStream() + + execOperations.exec { + it.commandLine 'git', 'rev-parse', '--short=8', 'HEAD' + it.standardOutput = output + } + return output.toString().trim() + } catch (GradleException ignore) { + // Git executable unavailable, or we are not building in a git repo. Fall through: + } + return "unknown" + } +} + +// Export closure +ext.getGitSha = { + providers.of(GitShaValueSource) {}.get() +}