Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Use git show-ref to retrieve hash reference #1840

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -561,25 +561,24 @@ def calculateVersion() {
}

def getCheckedOutGitCommitHash() {
def gitFolder = "$projectDir/.git/"
if (!file(gitFolder).isDirectory()) {
// We are in a submodule. The file's contents are `gitdir: <gitFolder>\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
getGitHeadHash().take 8
}

if (isCommit) return head[0].trim().take(takeFromHash) // e5a7c79edabb
def getGitHeadHash() {
getGitRefHash('^HEAD$')
}

def refHead = new File(gitFolder + head[1].trim()) // .git/refs/heads/master
refHead.text.trim().take takeFromHash
def getGitRefHash(String ref) {
def gitFolder = "$projectDir/.git/"
def cmd = "git --git-dir $gitFolder show-ref --head --hash $ref"
def proc = cmd.execute()
def errors = ""
proc.err.eachLine { line -> errors += "$line\n" }
proc.waitFor()
if (proc.exitValue() != 0) {
throw new GradleException("Unable to retrieve git ref hash for \"$ref\":\n$errors")
}
proc.in.readLines().get(0).trim()
}

tasks.register("verifyDistributions") {
Expand Down