forked from mycelium-com/wallet-android
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tools.gradle
71 lines (60 loc) · 2.49 KB
/
tools.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def getCheckedOutGitCommitHash() {
def gitFolder = project.rootDir.getPath() + "/.git/"
def takeFromHash = 12 // take the first n chars from the commit hash
/*
* Simple access to the current commit-hash, without having to rely on a local git installation
* (based on https://gist.github.com/JonasGroeger/7620911 )
*
* '.git/HEAD' contains either
* in case of detached head: the currently checked out commit hash ("3b3fe8f99d45d48ada83596cee371b21899d97a8")
* otherwise: a reference to a file containing the current commit hash ("ref: refs/heads/master")
*/
def hash = ""
try {
def head = new File(gitFolder + "HEAD").text.split(":") // .git/HEAD
def isCommit = (head.length == 1)
if (isCommit) {
hash = head[0].trim();
} else {
def refHead = new File(gitFolder + head[1].trim()) // .git/refs/heads/master
hash = refHead.text.trim()
}
}catch(IOException ex){
hash = ""
logger.warn("Unable to detect current git commit hash: {}", ex.toString())
}
def version = hash.take(takeFromHash)
logger.debug("Detected '{}' as git commit hash - using {} as version", hash, version)
return version
}
def getGitBranch() {
def gitFolder = project.rootDir.getPath() + "/.git/"
/*
* Simple access to the current branch, without having to rely on a local git installation
* (based on https://gist.github.com/JonasGroeger/7620911 )
*
* '.git/HEAD' contains either
* in case of detached head: the currently checked out commit hash ("3b3fe8f99d45d48ada83596cee371b21899d97a8")
* otherwise: a reference to a file containing the current commit hash ("ref: refs/heads/master")
*/
def branch = ""
try {
def head = new File(gitFolder + "HEAD").text.split(":") // .git/HEAD
def isDetached = (head.length == 1)
if (isDetached) {
logger.warn("Unable to detect current git branch, {}", head)
} else {
def refHead = new File(gitFolder + head[1].trim()) // .git/refs/heads/master
branch = head.last().split("/").last().trim();
}
}catch(IOException ex){
logger.warn("Unable to detect current git branch: {}", ex.toString())
}
logger.debug("Detected '{}' as git branch", branch)
return branch
}
// export the functions
ext{
getCheckedOutGitCommitHash = this.&getCheckedOutGitCommitHash
getGitBranch = this.&getGitBranch
}