From f224e9f5c0d4b1f6a64bde558d95a235d5415405 Mon Sep 17 00:00:00 2001 From: Danno Ferrin Date: Mon, 27 Jan 2020 01:58:11 -0700 Subject: [PATCH] Fix zip building (#327) * Fix zip build Currently building from a non-git checkout, such as a zip file, does not work because of the way we calculate the git commit hash. Add a catch clause to return 8 "x"s in case the calculation fails. Signed-off-by: Danno Ferrin * let the user know retrievial failed Signed-off-by: Danno Ferrin * left in debugging code tripping the error Signed-off-by: Danno Ferrin --- build.gradle | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/build.gradle b/build.gradle index ffe8cd1fee..8bb5e06a59 100644 --- a/build.gradle +++ b/build.gradle @@ -13,6 +13,8 @@ * SPDX-License-Identifier: Apache-2.0 */ + +import groovy.transform.Memoized import net.ltgt.gradle.errorprone.CheckSeverity import java.text.SimpleDateFormat @@ -637,6 +639,7 @@ def buildTime() { // Takes the version, and if -SNAPSHOT is part of it replaces SNAPSHOT // with the git commit version. +@Memoized def calculateVersion() { String version = rootProject.version if (version.endsWith("-SNAPSHOT")) { @@ -646,25 +649,31 @@ def calculateVersion() { } def getCheckedOutGitCommitHash() { - def gitFolder = "$projectDir/.git/" - if (!file(gitFolder).isDirectory()) { - // We are in a submodule. The file's contents are `gitdir: \n`. - // Read the file, cut off the front, and trim the whitespace. - gitFolder = file(gitFolder).text.substring(8).trim() + "/" + try { + def gitFolder = "$projectDir/.git/" + if (!file(gitFolder).isDirectory()) { + // We are in a submodule. The file's contents are `gitdir: \n`. + // Read the file, cut off the front, and trim the whitespace. + gitFolder = file(gitFolder).text.substring(8).trim() + "/" + } + def takeFromHash = 8 + /* + * '.git/HEAD' contains either + * in case of detached head: the currently checked out commit hash + * otherwise: a reference to a file containing the current commit hash + */ + def head = new File(gitFolder + "HEAD").text.split(":") // .git/HEAD + def isCommit = head.length == 1 // e5a7c79edabbf7dd39888442df081b1c9d8e88fd + + if (isCommit) return head[0].trim().take(takeFromHash) // e5a7c79edabb + + def refHead = new File(gitFolder + head[1].trim()) // .git/refs/heads/master + refHead.text.trim().take takeFromHash + } catch (Exception e) { + logger.warn('Could not calculate git commit, using "xxxxxxxx" (run with --info for stacktrace)') + logger.info('Error retrieving git commit', e) + return "xxxxxxxx" } - def takeFromHash = 8 - /* - * '.git/HEAD' contains either - * in case of detached head: the currently checked out commit hash - * otherwise: a reference to a file containing the current commit hash - */ - def head = new File(gitFolder + "HEAD").text.split(":") // .git/HEAD - def isCommit = head.length == 1 // e5a7c79edabbf7dd39888442df081b1c9d8e88fd - - if (isCommit) return head[0].trim().take(takeFromHash) // e5a7c79edabb - - def refHead = new File(gitFolder + head[1].trim()) // .git/refs/heads/master - refHead.text.trim().take takeFromHash } tasks.register("verifyDistributions") {