Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ checkout scmGit(
userRemoteConfigs: [[url: 'https://github.com/jenkinsci/ws-cleanup-plugin']])
----

[source,groovy]
----
def resolvedSCMBranch = resolveScm(
source: gitSource(
credentialsId: 'git-cred',
traits: [cloneOption(cloneOption(depth: 1, shallow: true)), gitBranchDiscovery()]
remote: 'https://github.com/jenkinsci/ws-cleanup-plugin'),
targets: ['main', 'master']
)
checkout resolvedSCMBranch
----

==== Checkout with a narrow refspec

Checkout from the workspace cleanup plugin source repository using https without credentials, the `master` branch, and with a refspec specific to the master branch.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ private <T, C extends GitSCMSourceContext<C, R>, R extends GitSCMSourceRequest>
fetch.tags(context.wantTags());
}
fetch = fetch.prune(prune);
fetch = fetch.shallow(context.wantShallow()).depth(context.depth());

URIish remoteURI = null;
try {
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/jenkins/plugins/git/GitSCMSourceContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public class GitSCMSourceContext<C extends GitSCMSourceContext<C, R>, R extends
*/
@NonNull
private String remoteName = AbstractGitSCMSource.DEFAULT_REMOTE_NAME;
/**
* Perform shallow clone
*/
private boolean shallow;
/**
* Shallow clone depth
*/
private Integer depth;

/**
* Constructor.
Expand Down Expand Up @@ -192,6 +200,25 @@ public final String remoteName() {
return remoteName;
}

/**
* Returns {@code true} if a limited number of commits will be retrieved when cloning with a GitSCMSource.
* A GitSCMSource is most commonly used when defining a multibranch Pipeline.
*
* @return {@code true} if a limited number of commits will be retrieved
*/
public final boolean wantShallow() {
return shallow;
}

/**
* Returns shallow clone depth
*
* @return shallow clone depth
*/
public final Integer depth() {
return depth;
}

/**
* Adds a requirement for branch details to any {@link GitSCMSourceRequest} for this context.
*
Expand Down Expand Up @@ -358,6 +385,34 @@ public final List<RefSpec> asRefSpecs() {
return result;
}

/**
* Limit the number of commits that will be retrieved when the multibranch Pipeline is cloned.
* When shallow clone is enabled, a single commit will be retrieved instead of retrieving all commits.
* If more commits are needed, set the depth to a larger value.
*
* @param shallow {@code true} to perform shallow clone
* @return {@code this} for method chaining.
*/
@SuppressWarnings("unchecked")
@NonNull
public final C shallow(boolean shallow) {
this.shallow = shallow;
return (C) this;
}

/**
* Configures shallow clone depth
*
* @param depth upper limit to the number of commits included in the repository clone
* @return {@code this} for method chaining.
*/
@SuppressWarnings("unchecked")
@NonNull
public final C depth(Integer depth) {
this.depth = depth;
return (C) this;
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import hudson.Extension;
import hudson.plugins.git.extensions.impl.CloneOption;
import jenkins.plugins.git.GitSCMSourceContext;
import jenkins.scm.api.trait.SCMSourceContext;
import jenkins.scm.api.trait.SCMSourceTrait;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
Expand All @@ -47,6 +49,12 @@ public CloneOptionTrait(CloneOption extension) {
super(extension);
}

@Override
protected void decorateContext(SCMSourceContext<?, ?> context) {
CloneOption extension = getExtension();
((GitSCMSourceContext<?, ?>) context).shallow(extension.isShallow()).depth(extension.getDepth());
}

/**
* Our {@link hudson.model.Descriptor}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package jenkins.plugins.git.traits;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import hudson.plugins.git.extensions.impl.CloneOption;
import jenkins.plugins.git.GitSCMSourceContext;

class CloneOptionTraitTest {

@Test
void testShallowDisabledByDefault() {
GitSCMSourceContext<?, ?> context = new GitSCMSourceContext<>(null, null);
assertFalse(context.wantShallow());
assertNull(context.depth());
}

@Test
void testDecorateWithDefaultCloneOption() {
GitSCMSourceContext<?, ?> context = new GitSCMSourceContext<>(null, null);
CloneOption cloneOption = new CloneOption(false, null, null);
CloneOptionTrait cloneOptionTrait = new CloneOptionTrait(cloneOption);
cloneOptionTrait.decorateContext(context);
assertFalse(context.wantShallow());
assertNull(context.depth());
}

@Test
void testDecorateCloneOptionWithShallow() {
GitSCMSourceContext<?, ?> context = new GitSCMSourceContext<>(null, null);
CloneOption cloneOption = new CloneOption(true, null, null);
cloneOption.setDepth(10);
CloneOptionTrait cloneOptionTrait = new CloneOptionTrait(cloneOption);
cloneOptionTrait.decorateContext(context);
assertTrue(context.wantShallow());
assertEquals(context.depth(), 10);
}
}