Skip to content

Commit

Permalink
Fetch specified commit explicitly via the refspec
Browse files Browse the repository at this point in the history
This fixes issues with building commits that are not fetched by the
default refspec of +refs/heads/*:refs/remotes/origin/*, eg. if the
specified commit is not in the history of any extant branch, or when
using shallow clones.

Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
  • Loading branch information
bensze01 committed Dec 4, 2024
1 parent 42d198f commit f560831
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion vars/checkout_repo.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import hudson.model.Result
import hudson.plugins.git.GitSCM
import hudson.scm.NullSCM
import jenkins.model.CauseOfInterruption
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
Expand Down Expand Up @@ -129,18 +130,49 @@ Map<String, String> checkout_mbed_os_example_repo(String repo, String branch) {
return checkout_report_errors(scm_config)
}

/** Produce an object that can be passed to {@code checkout} to make a shallow clone of the specified branch.
*
* @param repo
* URL of the Git repo.
*
* @param branch
* The branch / commit / tag to check out. Supports a variety of formats accepted by
* {@code git rev-parse}, eg.:
* <ul>
* <li>{@code <branchName>}
* <li>{@code refs/heads/<branchName>}
* <li>{@code origin/<branchName>}
* <li>{@code remotes/origin/<branchName>}
* <li>{@code refs/remotes/origin/<branchName>}
* <li>{@code <tagName>}
* <li>{@code refs/tags/<tagName>}
* <li>{@code refs/pull/<pullNr>/head}
* <li>{@code <commitId>}
* <li>{@code ${ENV_VARIABLE}}
* </ul>
* See also:
* <a href="https://www.jenkins.io/doc/pipeline/steps/params/scmgit/#scmgit">
* the documentation of the Git Plugin.
* </a>
*
* @return
* A {@link Map} representing a {@link GitSCM} object.
*/
Map<String, Object> parametrized_repo(String repo, String branch) {
String remoteRef = branch.replaceFirst('^((refs/)?remotes/)?origin/', '')
String localBranch = branch.replaceFirst('^(refs/)?(heads/|tags/|(remotes/)?origin/)?','')
return [
$class: 'GitSCM',
userRemoteConfigs: [[
url: repo,
refspec: "+$remoteRef:refs/remotes/origin/$localBranch",
credentialsId: env.GIT_CREDENTIALS_ID
]],
branches: [[name: branch]],
extensions: [
[$class: 'CloneOption', timeout: 60, honorRefspec: true, shallow: true],
[$class: 'SubmoduleOption', recursiveSubmodules: true, parentCredentials: true, shallow: true],
[$class: 'LocalBranch', localBranch: '**'],
[$class: 'LocalBranch', localBranch: localBranch],
],
]
}
Expand Down

0 comments on commit f560831

Please sign in to comment.