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 paths) { ArrayList sparsecheckoutpaths = new ArrayList() 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 ${reporootname}" 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 ${reporootname}" } } return reporootsubfolder } def gitCheckout(String gitURL, String revision, String targetDir, String repoRootSuffix = "", int cloneDepth = 1, boolean shallowClone = true, ArrayList sparseitemlist = new ArrayList()) { 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