Skip to content

Commit

Permalink
Issue #818: do shallow clone while retaining checkout abilitiy to spe…
Browse files Browse the repository at this point in the history
…cific SHA
  • Loading branch information
piyush kumar sadangi authored and piyush kumar sadangi committed Apr 4, 2024
1 parent cdeb66c commit d7b023c
Showing 1 changed file with 68 additions and 14 deletions.
82 changes: 68 additions & 14 deletions checkstyle-tester/diff.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def getCliOptions(args) {
+ 'config file (required if baseConfig and patchConfig are not secified)')
g(longOpt: 'allowExcludes', required: false, 'Whether to allow excludes specified in the list of ' \
+ 'projects (optional, default is false)')
h(longOpt: 'useShallowClone', required: false, 'Whether to use shallow clones for repositories ' \
+ '(optional, default is false)')
l(longOpt: 'listOfProjects', args: 1, required: true, argName: 'path',
'Path to file which contains projects to test on (required)')
s(longOpt: 'shortFilePaths', required: false, 'Whether to save report file paths' \
Expand Down Expand Up @@ -264,6 +266,7 @@ def generateCheckstyleReport(cfg) {
def checkstyleConfig = cfg.checkstyleCfg
def checkstyleVersion = cfg.checkstyleVersion
def allowExcludes = cfg.allowExcludes
def useShallowClone = cfg.useShallowClone
def listOfProjectsFile = new File(cfg.listOfProjects)
def projects = listOfProjectsFile.readLines()
def extraMvnRegressionOptions = cfg.extraMvnRegressionOptions
Expand Down Expand Up @@ -340,23 +343,20 @@ def getCommitSha(commitId, repoType, srcDestinationDir) {
return sha.replace('\n', '')
}

def getCloneCmd(repoType, repoUrl, srcDestinationDir) {
def cloneCmd = ''
switch (repoType) {
case 'git':
cloneCmd = "git clone $repoUrl $srcDestinationDir"
break
default:
throw new IllegalArgumentException("Error! Unknown $repoType repository.")
}
return cloneCmd
}

def cloneRepository(repoName, repoType, repoUrl, commitId, srcDir) {
println "In clone repository"
def srcDestinationDir = getOsSpecificPath("$srcDir", "$repoName")
if (!Files.exists(Paths.get(srcDestinationDir))) {
def cloneCmd = getCloneCmd(repoType, repoUrl, srcDestinationDir)
final String cloneCmd
if (useShallowClone && !isGitSha(commitId)) {
cloneCmd = getCloneShallowCmd(repoType, repoUrl, srcDestinationDir, commitId)
println "Clone command: $cloneCmd"
} else {
cloneCmd = getCloneCmd(repoType, repoUrl, srcDestinationDir)
println "Clone command: $cloneCmd"
}
println "Cloning $repoType repository '$repoName' to $srcDestinationDir folder ..."
println "Cloning command: $cloneCmd"
executeCmdWithRetry(cloneCmd)
println "Cloning $repoType repository '$repoName' - completed\n"
}
Expand All @@ -365,14 +365,59 @@ def cloneRepository(repoName, repoType, repoUrl, commitId, srcDir) {
def lastCommitSha = getLastProjectCommitSha(repoType, srcDestinationDir)
def commitIdSha = getCommitSha(commitId, repoType, srcDestinationDir)
if (lastCommitSha != commitIdSha) {
if (!isGitSha(commitId)) {
// If commitId is a branch or tag, fetch more data and then reset
fetchAdditionalData(repoType, srcDestinationDir, commitId)
}
def resetCmd = getResetCmd(repoType, commitId)
println "Resetting $repoType sources to commit '$commitId'"
executeCmd(resetCmd, new File("$srcDestinationDir"))
}
}

println "$repoName is synchronized"
}

def getCloneCmd(repoType, repoUrl, srcDestinationDir) {
final String cloneCmd = ''
switch (repoType) {
case 'git':
cloneCmd = "git clone $repoUrl $srcDestinationDir"
break
default:
throw new IllegalArgumentException("Error! Unknown $repoType repository.")
}
return cloneCmd
}

def getCloneShallowCmd(repoType, repoUrl, srcDestinationDir, commitId) {
if ('git'.equals(repoType)) {
return "git clone --depth 1 --branch $commitId $repoUrl $srcDestinationDir"
}
throw new IllegalArgumentException("Error! Unknown repository type: $repoType")
}

def fetchAdditionalData(repoType, srcDestinationDir, commitId) {
def fetchCmd = ''
if ('git'.equals(repoType)) {
if (isGitSha(commitId)) {
fetchCmd = "git fetch"
} else {
fetchCmd = "git fetch origin $commitId:$commitId"
}
}
else {
throw new IllegalArgumentException("Error! Unknown $repoType repository.")
}

executeCmd(fetchCmd, new File(srcDestinationDir))
}

// it is not very accurate match, but in case of mismatch we will do full clone
def isGitSha(value) {
return value ==~ /[0-9a-f]{5,40}/
}

def executeCmdWithRetry(cmd, dir = new File("").getAbsoluteFile(), retry = 5) {
def osSpecificCmd = getOsSpecificCmd(cmd)
def left = retry
Expand Down Expand Up @@ -708,7 +753,11 @@ def getResetCmd(repoType, commitId) {
def resetCmd = ''
switch (repoType) {
case 'git':
resetCmd = "git reset --hard $commitId"
if (isGitSha(commitId)) {
resetCmd = "git reset --hard $commitId"
} else {
resetCmd = "git reset --hard refs/tags/$commitId"
}
break
default:
throw new IllegalArgumentException("Error! Unknown $repoType repository.")
Expand Down Expand Up @@ -755,6 +804,7 @@ class Config {
def checkstyleVersion
def sevntuVersion
def allowExcludes
def useShallowClone

Config(cliOptions) {
if (cliOptions.localGitRepo) {
Expand All @@ -767,6 +817,7 @@ class Config {

checkstyleVersion = cliOptions.checkstyleVersion
allowExcludes = cliOptions.allowExcludes
useShallowClone = cliOptions.useShallowClone

mode = cliOptions.mode
if (!mode) {
Expand Down Expand Up @@ -815,6 +866,7 @@ class Config {
destDir: tmpMasterReportsDir,
extraMvnRegressionOptions: extraMvnRegressionOptions,
allowExcludes:allowExcludes,
useShallowClone: useShallowClone,
]
}

Expand All @@ -827,6 +879,7 @@ class Config {
destDir: tmpPatchReportsDir,
extraMvnRegressionOptions: extraMvnRegressionOptions,
allowExcludes: allowExcludes,
useShallowClone: useShallowClone,
]
}

Expand All @@ -840,6 +893,7 @@ class Config {
shortFilePaths: shortFilePaths,
mode: mode,
allowExcludes: allowExcludes,
useShallowClone: useShallowClone,
]
}
}
Expand Down

0 comments on commit d7b023c

Please sign in to comment.