firmware_tools/versioncontrol/git.groovy
Dan Hawkins ab3516d23d Squashed 'Common_scripts/' changes from 0326879..b70fe5f
b70fe5f Remove debug release filtering
3730461 Debug release filtering
db92ee3 Debug release filtering
2393146 Use releasetag script
54bf29f Attempt to separate dependent products and sub-components
3c4de23 Debugging script
d93f942 Debugging script
caf71e3 Debugging script
c993ecc Debugging script
2e5b6b2 Debugging script
0ad763c Debugging script
d5a178b Debugging script
01d91a7 Debugging script
d2a22db Debugging script
30ac58d Debugging script
490b37b Debugging script
2939eaf Debugging script
35d3862 Debugging script
defc807 Debugging script
f2e3e52 Debugging script
b5e2894 Debugging script
c2faf8a Debugging script
fa199eb Debugging script
d418e5a Debugging script
f34fb2e Debugging script
04e947c Debugging script
48d7b0f Debugging script
38b562b Debugging script
ac5e7cd Debugging script
1d3f184 Debugging script
b187d23 Debugging script
1131865 Debugging script
68cbc00 Debugging script
5771490 Debugging
ed17de0 Debugging
b1bc540 Debugging
e6c6fcf Debugging
57eeaf4 Comment out code to debug later
a576d64 Script reorg
32a3aed Supply release tag processing
01ce780 Supply release tag processing
af3e22d Supply release tag processing
b7c5a66 Supply release tag processing
4fbe1d9 Supply release tag processing
44b1e13 Supply release tag processing
af3c041 Supply release tag processing
8b361de Supply release tag processing
89736f0 Supply release tag processing
70d569c Supply release tag processing
d2b8d1b Supply release tag processing
ce33f59 Supply release tag processing
88cba3b Supply release tag processing
80bc64e Supply release tag processing
6488203 Supply release tag processing
c2b27a8 Merge commit 'cd98f833a8db023604d045d9343f18f3a423da5c'
704e499 Start adding dependencies
940a8d5 Start adding dependencies
4a46b8f Build up a tag map
0ca7886 Build up a tag map
3dbdf85 Build up a tag map
fb72031 Build up a tag map
be599cb Build up a tag map
462b177 Build up a tag map
453a14a Build up a tag map
c5f01d0 Build up a tag map
11826a4 Get list of tags for each product release repo
3981ece Filter long name releases from version selection
cad2d4e Filter long name releases from version selection
8bd075f Filter long name releases from version selection
20c5648 Merge commit '6633e04d0257e6d52427b2ff761c8a5625196346' as 'Common_scripts'
9a31beb Remove folder
2767e8a Rename folder
a5ee31d New files
2c6a90d New files
e5d8c38 Merge commit 'c1344e9084' as 'Commons_scripts'
c1344e9 Squashed 'Commons_scripts/' content from commit 0326879
9171d4d Initial commit
e5bca0d Initial commit

git-subtree-dir: Common_scripts
git-subtree-split: b70fe5f7bd
2022-03-02 13:29:36 +00:00

400 lines
14 KiB
Groovy

import hudson.plugins.git.*
import hudson.plugins.git.extensions.*
import hudson.plugins.git.extensions.impl.*
clh = load("${autobuilder.commonscripts()}/commandline/commandline.groovy")
def boolean isShallowClonedRepo(String gitrootpath) {
def boolean isshallowclone = true
dir( gitrootpath) {
if (isUnix()) {
isshallowclone = sh(returnStdout: true, script: "git rev-parse --is-shallow-repository").trim().toBoolean()
}
else {
isshallowclone = powershell(returnStdout: true, script: "git rev-parse --is-shallow-repository").trim().toBoolean()
}
echo "Repo at ${gitrootpath} is shallow cloned: ${isshallowclone}"
}
return isshallowclone
}
String getGitInfo(String workingSetRootPath) {
dir(workingSetRootPath) {
return clh.getCommandOutput('git rev-parse HEAD')
}
}
ArrayList<String> gettaglist(String workingSetRootPath) {
ArrayList<String> taglist = new ArrayList<String>()
dir(workingSetRootPath) {
def tagliststring = clh.getCommandOutput('git tag --sort=-version:refname')
taglist = tagliststring.split('\\n')
}
return taglist
}
LinkedHashMap<String, ArrayList<String>> getremoteshatagmap(String repositoryURL) {
def shatagliststring = clh.getCommandOutput("git ls-remote --tags --sort=-version:refname ${repositoryURL}")
def shataglist = shatagliststring.split('\\n')
LinkedHashMap<String, ArrayList<String>> shatagmap = new LinkedHashMap<String, ArrayList<String>>()
for (shatag in shataglist) {
def shatagfields = shatag.split('\\t')
def sha = shatagfields[0].trim()
def tag = shatagfields[1].trim()
echo "sha: ${sha} tag: ${tag}"
if (shatagmap.get(sha) == null) {
def ArrayList<String> taglist = new ArrayList<String>()
taglist.add(tag)
shatagmap.put(sha, taglist)
} else {
def taglist = shatagmap.get(sha)
taglist.add(tag)
shatagmap.put(sha, taglist)
}
}
return shatagmap
}
@NonCPS
def ArrayList<SparseCheckoutPath> sparsecheckoutconfig(ArrayList<String> paths) {
ArrayList<SparseCheckoutPath> sparsecheckoutpaths = new ArrayList<SparseCheckoutPath>()
for (path in paths) {
sparsecheckoutpaths.add(new SparseCheckoutPath(path))
}
return 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}"
/*
dir(reporootsubfolder) {
if (isUnix()) {
sh """
git status
if [ \$? -ne 0 ]; then exit 1; fi
"""
} else {
powershell """
git status
if (\$lastexitcode -ge 1) { exit 1 }
"""
}
}
*/
}
}
return reporootsubfolder
}
def checkgitlfsinstall() {
if (isUnix()) {
sh """
git lfs install
if [ \$? -ne 0 ]; then exit 1; fi
"""
} else {
powershell """
git lfs install
if (\$lastexitcode -ge 1) { exit 1 }
"""
}
}
//No credentials specified
//Cloning the remote Git repository
//Cloning repository git@hydra:print-server-releases/print_server_whole.git
//> git init /home/jenkins_user/jenkins/jobs/Print_Server_publish_to_customer/ws/whole # timeout=10
//Using reference repository: /home/jenkins_user/reporoot/git_hydra_print_server_releases_print_server_whole_git
//Fetching upstream changes from git@hydra:print-server-releases/print_server_whole.git
// > git --version # timeout=10
// > git fetch --tags --progress git@hydra:print-server-releases/print_server_whole.git +refs/heads/*:refs/remotes/origin/* # timeout=120
// > git config remote.origin.url git@hydra:print-server-releases/print_server_whole.git # timeout=10
// > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
// > git config remote.origin.url git@hydra:print-server-releases/print_server_whole.git # timeout=10
// Cleaning workspace
// > git rev-parse --verify HEAD # timeout=10
// No valid HEAD. Skipping the resetting
// > git clean -fdx # timeout=10
// Fetching upstream changes from git@hydra:print-server-releases/print_server_whole.git
// > git fetch --tags --progress git@hydra:print-server-releases/print_server_whole.git +refs/heads/*:refs/remotes/origin/* # timeout=120
// > git rev-parse refs/tags/2.10180.7389.21125_7^{commit} # timeout=10
// > git rev-parse refs/remotes/origin/refs/tags/2.10180.7389.21125_7^{commit} # timeout=10
// Checking out Revision 64cac35e9a7603c060135f61f91b3f809bbdf160 (refs/tags/2.10180.7389.21125_7)
// Enabling Git LFS pull
// > git config core.sparsecheckout # timeout=10
// > git checkout -f 64cac35e9a7603c060135f61f91b3f809bbdf160 # timeout=120
// > git config --get remote.origin.url # timeout=10
// > git lfs pull origin # timeout=120
// Commit message: "2.10180.7389.21125_7"
// > git rev-list --no-walk 64cac35e9a7603c060135f61f91b3f809bbdf160 # timeout=10
// > git rev-parse --is-inside-work-tree # timeout=10
// Fetching changes from the remote Git repository
// > git config remote.origin.url git@hydra:releases/print-server-installer.git # timeout=10
// Cleaning workspace
// > git rev-parse --verify HEAD # timeout=10
// Resetting working tree
// > git reset --hard # timeout=10
// > git clean -fdx # timeout=10
// Using shallow fetch with depth 1
// Fetching upstream changes from git@hydra:releases/print-server-installer.git
// > git --version # timeout=10
// > git fetch --tags --progress git@hydra:releases/print-server-installer.git +refs/heads/*:refs/remotes/origin/* --depth=1 # timeout=120
// > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
// > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
// Checking out Revision d0788d22622862e441c5995a22629846573c39f3 (refs/remotes/origin/master)
// Enabling Git LFS pull
// > git config core.sparsecheckout # timeout=10
// > git read-tree -mu HEAD # timeout=10
// > git checkout -f d0788d22622862e441c5995a22629846573c39f3 # timeout=120
// > git config --get remote.origin.url # timeout=10
// > git lfs pull origin # timeout=120
// Commit message: "2.10180.7389.21125_3"
// > git rev-list --no-walk d0788d22622862e441c5995a22629846573c39f3 # timeout=10
def String lfsinstallandreporoot(String gitURL) {
checkgitlfsinstall()
return checkouttoreporoot(gitURL)
}
def showsparseitemlist(ArrayList<String> sparseitemlist = new ArrayList<String>()) {
if (!sparseitemlist.isEmpty()) {
def itemstring = "Items in sparse item list:\n"
for (item in sparseitemlist) {
itemstring += item + "\n"
}
echo itemstring
}
}
def docheckout(String gitURL, String revision, int cloneDepth, String reporootpath, boolean shallowClone, String targetDir, sparseitemlist) {
echo "Git: Checking out: URL: ${gitURL} Revision: ${revision} to: ${targetDir}"
checkout([
$class: 'GitSCM',
userRemoteConfigs: [
[url: gitURL]
],
branches: [
[name: revision]
],
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],
[$class: 'SparseCheckoutPaths', sparseCheckoutPaths: sparsecheckoutconfig(sparseitemlist)],
[$class: 'PruneStaleBranch'],
],
submoduleCfg: [],
])
}
// gitCheckout (below) cannot handle sparse checkout and lfs at the moment - see https://issues.jenkins-ci.org/browse/JENKINS-43427 - see gitCheckoutAlt()
def gitCheckout(String gitURL, String revision, String targetDir, String repoRootSuffix = "", int cloneDepth = 1, boolean shallowClone = true, ArrayList<String> sparseitemlist = new ArrayList<String>()) {
def String reporootpath = lfsinstallandreporoot(gitURL)
showsparseitemlist(sparseitemlist)
docheckout(gitURL, revision, cloneDepth, reporootpath, shallowClone, targetDir, sparseitemlist)
}
def gitinit(String targetDir, String reporootpath) {
if (!fileExists("${targetDir}/.git")) { // Git folder does not already exist
echo "git init ${targetDir}"
if (isUnix()) {
sh """
git init "${targetDir}"
"""
} else {
powershell """
git init "${targetDir}"
"""
}
}
echo "Writing ${reporootpath}/.git/objects to ${targetDir}/.git/objects/info/alternates"
dir("${targetDir}/.git/objects/info") {
writeFile(file: 'alternates', text: "${reporootpath}/.git/objects")
}
}
def setlfsfetchinclude(String targetDir, ArrayList<String> sparseitemlist) {
def String sparseitemliststring = ""
if (!sparseitemlist.isEmpty()) {
for (item in sparseitemlist) {
sparseitemliststring += "${item},"
}
echo "git config lfs.fetchinclude ${sparseitemliststring}"
dir(targetDir) {
if (isUnix()) {
sh """
git config lfs.fetchinclude "${sparseitemliststring}"
"""
} else {
powershell """
git config lfs.fetchinclude "${sparseitemliststring}"
"""
}
}
}
}
def gitsparserestore(String targetDir) {
echo "Clean out full fetch; Force a sparse fetch"
dir(targetDir) {
if (isUnix()) {
sh """
git rm -rf . > /dev/null
git clean -fxd > /dev/null
git reset --hard > /dev/null
"""
} else {
powershell """
git rm -rf . > \$null
git clean -fxd > \$null
git reset --hard > \$null
"""
}
}
}
def List convertSparseItemsString(String itemstopublish) {
def List sparsepaths = []
boolean nonempty = itemstopublish?.trim()
if (nonempty) {
def String[] items = itemstopublish.trim().split(',')
def String allpaths = ""
for(String item : items) {
sparsepaths.add(item)
allpaths += "${item}\n"
}
echo "Selected item to publish:\n${allpaths}"
}
return sparsepaths
}
def gitCheckoutAlt(String gitURL, String revision, String targetDir, String repoRootSuffix = "", int cloneDepth = 1, boolean shallowClone = true, ArrayList<String> sparseitemlist = new ArrayList<String>()) {
echo "Performing alternative checkout"
def String reporootpath = lfsinstallandreporoot(gitURL)
showsparseitemlist(sparseitemlist)
if (!sparseitemlist.isEmpty()) {
gitinit(targetDir, reporootpath)
setlfsfetchinclude(targetDir, sparseitemlist)
}
docheckout(gitURL, revision, cloneDepth, reporootpath, shallowClone, targetDir, sparseitemlist)
if (!sparseitemlist.isEmpty()) {
gitsparserestore(targetDir)
}
}
def checkRepoExists(String gitURL) {
def int result = 0
if (isUnix()) {
result = sh( script: """
git ls-remote "${gitURL}" CHECK_GIT_REMOTE_URL_REACHABILITY
""",
returnStatus: true
)
} else {
result = powershell( script: """
git ls-remote "${gitURL}" CHECK_GIT_REMOTE_URL_REACHABILITY
""",
returnStatus: true
)
}
echo "git ls-remote return result: ${result}"
if (result > 0) {
error("The release repo: ${gitURL} cannot be found. Please ensure this exists before building")
} else {
echo "The release repo: ${gitURL} exists"
}
}
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) {
if (isUnix()) {
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
find . -iname "*@tmp" | xargs -iXX rm -rf XX
"""
} else {
echo "Not supporting git stripping in Windows at the moment"
}
}
}
return this