firmware_tools/versioncontrol/git.groovy

160 lines
5 KiB
Groovy

import hudson.plugins.git.*
import hudson.plugins.git.extensions.*
import hudson.plugins.git.extensions.impl.*
clh = load("${autobuilder.commonscripts()}/commandline/commandline.groovy")
String getGitInfo(String workingSetRootPath) {
dir(workingSetRootPath) {
return clh.getCommandOutput('git rev-parse HEAD')
}
}
@NonCPS
def SparseCheckoutPaths sparsecheckoutconfig(ArrayList<String> paths) {
ArrayList<SparseCheckoutPath> sparsecheckoutpaths = new ArrayList<SparseCheckoutPath>()
for (path in paths) {
sparsecheckoutpaths.add(new SparseCheckoutPath(path))
}
return new SparseCheckoutPaths(sparsecheckoutpaths)
}
def String uniquereporootname(String gitURL) {
def uniquename = gitURL.replaceAll("[^A-Za-z0-9]", "_")
echo "Unique reporoot name for URL: ${gitURL} is: ${uniquename}"
return uniquename
}
// The REPO_ROOT is a folder where a cache of git checkouts can be kept - each subfolder name is unique to a particular git repo
def String checkouttoreporoot(String gitURL) {
def String reporootsubfolder
def String reporootenv = env.REPO_ROOT
if (!reporootenv) {
reporootsubfolder = ""
echo "To improve Git performance: Define the REPO_ROOT enviroment to a path where a cache of git repositories can be placed on this slave"
} else {
def reporootname = uniquereporootname(gitURL)
reporootsubfolder = "${reporootenv}/${reporootname}"
if (!fileExists(reporootsubfolder)) {
echo "Checking out ${gitURL} to ${reporootsubfolder}"
checkout([
$class: 'GitSCM',
userRemoteConfigs: [
[url: gitURL]
],
branches: [
[name: 'origin/master']
],
doGenerateSubmoduleConfigurations: false,
extensions: [
[$class: 'GitLFSPull'],
[$class: 'CleanBeforeCheckout'],
[$class: 'CheckoutOption', timeout: 180],
[$class: 'RelativeTargetDirectory', relativeTargetDir: reporootsubfolder],
],
submoduleCfg: [],
])
} else {
echo "Found ${gitURL} checked out at ${reporootsubfolder} ... checking status"
dir(reporootsubfolder) {
if (isUnix()) {
sh """
git status
if [ \$? -ne 0 ]; then exit 1; fi
"""
} else {
bat """
git status
if errorlevel 1 EXIT /b 1
"""
}
}
}
}
return reporootsubfolder
}
def checkgitlfsinstall() {
if (isUnix()) {
sh """
git lfs install
if [ \$? -ne 0 ]; then exit 1; fi
"""
} else {
bat """
git lfs install
if errorlevel 1 EXIT /b 1
"""
}
}
def gitCheckout(String gitURL, String revision, String targetDir, String repoRootSuffix = "", int cloneDepth = 1, boolean shallowClone = true, ArrayList<String> sparseitemlist = new ArrayList<String>()) {
checkgitlfsinstall()
def reporootpath = checkouttoreporoot(gitURL)
checkout([
$class: 'GitSCM',
userRemoteConfigs: [
[url: gitURL]
],
branches: [
[name: revision]
],
doGenerateSubmoduleConfigurations: false,
extensions: [
[$class: 'GitLFSPull'],
[$class: 'CleanBeforeCheckout'],
[$class: 'CloneOption', depth: cloneDepth, noTags: false, reference: reporootpath, shallow: shallowClone, timeout: 120],
[$class: 'CheckoutOption', timeout: 120],
[$class: 'RelativeTargetDirectory', relativeTargetDir: targetDir],
sparsecheckoutconfig(sparseitemlist)
],
submoduleCfg: [],
])
}
String getSHAFromVersion(String versionFolder) {
dir (versionFolder) {
srcRevision = readFile("gitrevision.txt").trim()
}
return srcRevision
}
boolean checkAllVersionsAgree(java.util.LinkedHashMap versions) {
def keys = versions.keySet()
if (keys.size() < 1) {
return false
}
if (keys.size() == 1) {
return true
}
else {
version = ""
for (key in keys) {
nextversion = versions.get(key)
if (version == "") {
version = nextversion
}
if (nextversion != version) {
return false
}
}
}
return true
}
def stripgithistory(String rootfolder) {
echo "Stripping git history out of ${rootfolder}"
dir(rootfolder) {
sh """#!/bin/bash
pwd
find . -type d -name ".git" | xargs -iXX rm -rf XX
find . -name ".gitignore" | xargs -iXX rm -rf XX
find . -name ".gitmodules" | xargs -iXX rm -rf XX
find . -iname ".gitattributes" | xargs -iXX rm -rf XX
"""
}
}
return this