162 lines
No EOL
5.7 KiB
Groovy
162 lines
No EOL
5.7 KiB
Groovy
gh = load("${autobuilder.commonscripts()}/versioncontrol/git.groovy")
|
|
|
|
def int maxversionnumber() {
|
|
def int MAXVERSIONNUMBER = 65535
|
|
return MAXVERSIONNUMBER
|
|
}
|
|
|
|
int incrementrevision(int current, int increment) {
|
|
def int newrevision = (current + increment) % (maxversionnumber() + 1)
|
|
echo "Current revision: ${current} Increment: ${increment} New revision: ${newrevision}"
|
|
return newrevision
|
|
}
|
|
|
|
def getGitRevision(String gitrootpath, int basebuildnumber) {
|
|
def int adjustedchangecount
|
|
def String gitrevision
|
|
def String remoteinfo
|
|
def String remoteurl
|
|
|
|
def boolean isShallowCloned = gh.isShallowClonedRepo(gitrootpath)
|
|
if (isShallowCloned) {
|
|
error("Cannot generate a version number from a shallow cloned repo at ${gitrootpath}")
|
|
}
|
|
|
|
dir( gitrootpath) {
|
|
def int changecount = 0
|
|
if (isUnix()) {
|
|
changecount = sh(returnStdout: true, script: "git rev-list HEAD --count").trim().toInteger()
|
|
}
|
|
else {
|
|
changecount = bat(returnStdout: true, script: "@git rev-list HEAD --count").trim().toInteger()
|
|
}
|
|
echo "Change count: ${changecount}"
|
|
def int mvnp1 = maxversionnumber() + 1
|
|
basebuildnumber = (basebuildnumber < 0) ? mvnp1 - (basebuildnumber.abs() % mvnp1) : basebuildnumber
|
|
adjustedchangecount = incrementrevision(changecount, basebuildnumber)
|
|
echo "Adjusted change count: ${adjustedchangecount}"
|
|
if (isUnix()) {
|
|
gitrevision = sh(returnStdout: true, script: "git rev-parse HEAD").trim()
|
|
}
|
|
else {
|
|
gitrevision = bat(returnStdout: true, script: "@git rev-parse HEAD").trim()
|
|
}
|
|
echo "Revision: ${gitrevision}"
|
|
if (isUnix()) {
|
|
remoteinfo = sh(returnStdout: true, script: "git remote show origin").trim()
|
|
}
|
|
else {
|
|
remoteinfo = bat(returnStdout: true, script: "@git remote show origin").trim()
|
|
}
|
|
echo "Remote info: ${remoteinfo}"
|
|
if (isUnix()) {
|
|
remoteurl = sh(returnStdout: true, script: "git config --get remote.origin.url").trim()
|
|
}
|
|
else {
|
|
remoteurl = bat(returnStdout: true, script: "@git config --get remote.origin.url").trim()
|
|
}
|
|
echo "Remote URL: ${remoteurl}"
|
|
}
|
|
return [adjustedchangecount, gitrevision, remoteinfo, remoteurl]
|
|
}
|
|
|
|
// Handle:
|
|
// origin/master
|
|
// origin/2.0.x
|
|
def getVersionPrefix(String sourcebranch, boolean droptrailingdot = false) {
|
|
echo "Determining version number prefix from source branch name: ${sourcebranch}"
|
|
sourcebranch = sourcebranch.toLowerCase()
|
|
|
|
def defaultversionprefix = "1.30000.0."
|
|
def splits = sourcebranch.split('/')
|
|
if (splits.size() < 1) {
|
|
echo "Source branch name is invalid. Defaulting to: ${defaultversionprefix}"
|
|
return defaultversionprefix
|
|
}
|
|
|
|
def mastertypeversionprefix = "1.20000.0."
|
|
sourcebranch = splits.last()
|
|
if (sourcebranch.toLowerCase().find("master") != null) {
|
|
echo "Source branch name has 'master' in it. Defaulting to: ${mastertypeversionprefix}"
|
|
return mastertypeversionprefix
|
|
}
|
|
|
|
def splits2 = sourcebranch.split('\\.')
|
|
if (splits2.size() < 1) {
|
|
def thirdpartyversionprefix = "1.25000.0."
|
|
echo "Source branch name has no dots in it (eg. 1.9.2). Defaulting to: ${thirdpartyversionprefix}"
|
|
return thirdpartyversionprefix
|
|
}
|
|
|
|
for (split in splits2) {
|
|
if (split == "x") {
|
|
echo "Source branch name has '.x' or 'x.' in it. Defaulting to: ${mastertypeversionprefix}"
|
|
return mastertypeversionprefix
|
|
}
|
|
}
|
|
|
|
def integerstrings = sourcebranch.findAll(/\d+/)
|
|
def integers = []
|
|
for (integerstring in integerstrings) {
|
|
int integer = integerstring.toInteger()
|
|
integer = integer % (maxversionnumber() + 1)
|
|
integers.add(integer)
|
|
}
|
|
|
|
def versionprefix = ""
|
|
if (integers.size() < 1) {
|
|
echo "This source branch name has no numbers in its name so defaulting to: ${defaultversionprefix}"
|
|
versionprefix = defaultversionprefix
|
|
}
|
|
else {
|
|
def int integernumbercount = 0
|
|
for (integer in integers) {
|
|
versionprefix += integer + "."
|
|
integernumbercount += 1
|
|
if (integernumbercount > 2) {
|
|
break
|
|
}
|
|
}
|
|
if (integernumbercount == 0) {
|
|
echo "No leading numbers were found in source branch name so defaulting to: ${defaultversionprefix}"
|
|
versionprefix = defaultversionprefix
|
|
}
|
|
else if (integernumbercount == 1) {
|
|
versionprefix += "0.0."
|
|
}
|
|
else if (integernumbercount == 2) {
|
|
versionprefix += "0."
|
|
}
|
|
}
|
|
if (droptrailingdot == true) {
|
|
versionprefix = versionprefix.substring(0, versionprefix.length() - 1);
|
|
}
|
|
echo "Determined version prefix to be: ${versionprefix}"
|
|
return versionprefix
|
|
}
|
|
|
|
String generate(meta, String sourcerevision, String sourcepath, int basebuildnumber = 1000) {
|
|
def int gitrevisioncount
|
|
def String gitrevision
|
|
def String remoteinfo
|
|
def String remoteurl
|
|
|
|
(gitrevisioncount, gitrevision, remoteinfo, remoteurl) = getGitRevision(sourcepath, basebuildnumber)
|
|
|
|
def versionprefix = getVersionPrefix(sourcerevision)
|
|
|
|
def buildstamp = versionprefix + gitrevisioncount.toString()
|
|
echo "Build stamp is: ${buildstamp}"
|
|
|
|
meta.gitinfo.set(sourcepath, sourcerevision, gitrevision, remoteurl, remoteinfo)
|
|
meta.buildstampinfo.set(versionprefix, gitrevisioncount, buildstamp)
|
|
|
|
return buildstamp
|
|
}
|
|
|
|
String generate(meta, String sourcerevision, String sourcepath, String basebuildnumber) {
|
|
def int bbn = basebuildnumber.trim().toInteger()
|
|
return generate(meta, sourcerevision, sourcepath, bbn)
|
|
}
|
|
|
|
return this; |