This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.gradle
103 lines (91 loc) · 3.05 KB
/
git.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
def gitVersionNumber() {
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine "git", "describe", "--first-parent"
standardOutput = stdout
}
return stdout.toString().trim()
} catch (ignored) {
return "unknown"
}
}
def gitBranch() {
try {
if (System.env.CI?.trim()) {
// we run within Gitlab-CI, so there are no branches as it uses detached HEADs
if (System.env.CI_COMMIT_TAG?.trim()) {
// we are building a tag and therefore there is no branch given
return "built from tag"
}
// when not building a tag, the ref name is the branch
return System.env.CI_COMMIT_REF_NAME
}
// we run outside of Gitlab-CI so there should be a current branch
def stdout = new ByteArrayOutputStream()
exec {
commandLine "git", "rev-parse", "--abbrev-ref", "HEAD"
standardOutput = stdout
}
return stdout.toString().trim()
} catch (ignored) {
return "unknown"
}
}
def gitHash() {
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine "git", "rev-parse", "--short", "HEAD"
standardOutput = stdout
}
return stdout.toString().trim()
}
catch (ignored) {
return "0"
}
}
def fullVersionFromGitTag() {
def fullVersion = gitVersionNumber() + "-" + gitBranch()
println "Full version " + fullVersion
if (fullVersion != "unknown-HEAD") {
return fullVersion
}
println 'Setting dummy version...'
return "0.0.1"
}
def shortVersionFromGitTag() {
def gitSha = gitHash()
def shortExpr = ((fullVersionFromGitTag() =~ /(\D*)(\d+\.\d+\.\d+)(.*)/))
def shortVersion = shortExpr[0][2] + "-(" + gitSha + ")"
def tagOffsetMatcher = fullVersionFromGitTag() =~ /^.*-(\d+)-.*/
if (tagOffsetMatcher.find()) {
if (tagOffsetMatcher.groupCount() >= 1) {
shortVersion = shortExpr[0][2] + "." + tagOffsetMatcher.group(1) + "-(" + gitSha + ")"
}
}
return shortVersion
}
def numericVersionFromGitTag() {
def numVersionReg = ((fullVersionFromGitTag() =~ /(\D*)(\d+\.\d+\.\d+)(.*)/))[0][2]
def (major, minor, patch) = numVersionReg.split('\\.')
def tagOffset = 0
def tagOffsetMatcher = fullVersionFromGitTag() =~ /^.*-(\d+)-.*/
if (tagOffsetMatcher.find()) {
if (tagOffsetMatcher.groupCount() >= 1) {
tagOffset = tagOffsetMatcher.group(1) as Integer
}
}
if (minor.toInteger() >= 10) {
throw new StopActionException('Minor has to be lower 10')
}
return (major.toInteger() * 100000 + minor.toInteger() * 10000 + patch.toInteger() * 100 + tagOffset)
}
ext {
gitHash = this.&gitHash
gitBranch = this.&gitBranch
gitVersionNumber = this.&gitVersionNumber
shortVersionFromGitTag = this.&shortVersionFromGitTag
fullVersionFromGitTag = this.&fullVersionFromGitTag
numericVersionFromGitTag = this.&numericVersionFromGitTag
}