Skip to content

Commit 48a5ec4

Browse files
committed
implement decorateContext in CloneOptionTrait
1 parent fea27bd commit 48a5ec4

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

README.adoc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,17 @@ checkout scmGit(
136136
userRemoteConfigs: [[url: 'https://github.com/jenkinsci/ws-cleanup-plugin']])
137137
----
138138

139+
[source,groovy]
140+
----
141+
resolveScm(
142+
source: gitSource(
143+
credentialsId: 'git-cred',
144+
traits: [cloneOption(cloneOption(depth: 1, shallow: true)), gitBranchDiscovery()]
145+
remote: 'https://github.com/jenkinsci/ws-cleanup-plugin'),
146+
targets: ['main', 'master']
147+
)
148+
----
149+
139150
==== Checkout with a narrow refspec
140151

141152
Checkout from the workspace cleanup plugin source repository using https without credentials, the `master` branch, and with a refspec specific to the master branch.

src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ private <T, C extends GitSCMSourceContext<C, R>, R extends GitSCMSourceRequest>
404404
fetch.tags(context.wantTags());
405405
}
406406
fetch = fetch.prune(prune);
407+
fetch = fetch.shallow(context.wantShallow()).depth(context.depth());
407408

408409
URIish remoteURI = null;
409410
try {

src/main/java/jenkins/plugins/git/GitSCMSourceContext.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ public class GitSCMSourceContext<C extends GitSCMSourceContext<C, R>, R extends
9494
*/
9595
@NonNull
9696
private String remoteName = AbstractGitSCMSource.DEFAULT_REMOTE_NAME;
97+
/**
98+
* Perform shallow clone
99+
*/
100+
private boolean shallow;
101+
/**
102+
* Shallow clone depth
103+
*/
104+
private Integer depth;
97105

98106
/**
99107
* Constructor.
@@ -192,6 +200,25 @@ public final String remoteName() {
192200
return remoteName;
193201
}
194202

203+
/**
204+
* Returns {@code true} if a limited number of commits will be retrieved when cloning with a GitSCMSource.
205+
* A GitSCMSource is most commonly used when defining a multibranch Pipeline.
206+
*
207+
* @return {@code true} if a limited number of commits will be retrieved
208+
*/
209+
public final boolean wantShallow() {
210+
return shallow;
211+
}
212+
213+
/**
214+
* Returns shallow clone depth
215+
*
216+
* @return shallow clone depth
217+
*/
218+
public final Integer depth() {
219+
return depth;
220+
}
221+
195222
/**
196223
* Adds a requirement for branch details to any {@link GitSCMSourceRequest} for this context.
197224
*
@@ -358,6 +385,34 @@ public final List<RefSpec> asRefSpecs() {
358385
return result;
359386
}
360387

388+
/**
389+
* Limit the number of commits that will be retrieved when the multibranch Pipeline is cloned.
390+
* When shallow clone is enabled, a single commit will be retrieved instead of retrieving all commits.
391+
* If more commits are needed, set the depth to a larger value.
392+
*
393+
* @param shallow {@code true} to perform shallow clone
394+
* @return {@code this} for method chaining.
395+
*/
396+
@SuppressWarnings("unchecked")
397+
@NonNull
398+
public final C shallow(boolean shallow) {
399+
this.shallow = shallow;
400+
return (C) this;
401+
}
402+
403+
/**
404+
* Configures shallow clone depth
405+
*
406+
* @param depth upper limit to the number of commits included in the repository clone
407+
* @return {@code this} for method chaining.
408+
*/
409+
@SuppressWarnings("unchecked")
410+
@NonNull
411+
public final C depth(Integer depth) {
412+
this.depth = depth;
413+
return (C) this;
414+
}
415+
361416
/**
362417
* {@inheritDoc}
363418
*/

src/main/java/jenkins/plugins/git/traits/CloneOptionTrait.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
import hudson.Extension;
2929
import hudson.plugins.git.extensions.impl.CloneOption;
30+
import jenkins.plugins.git.GitSCMSourceContext;
31+
import jenkins.scm.api.trait.SCMSourceContext;
3032
import jenkins.scm.api.trait.SCMSourceTrait;
3133
import org.jenkinsci.Symbol;
3234
import org.kohsuke.stapler.DataBoundConstructor;
@@ -47,6 +49,12 @@ public CloneOptionTrait(CloneOption extension) {
4749
super(extension);
4850
}
4951

52+
@Override
53+
protected void decorateContext(SCMSourceContext<?, ?> context) {
54+
CloneOption extension = getExtension();
55+
((GitSCMSourceContext<?, ?>) context).shallow(extension.isShallow()).depth(extension.getDepth());
56+
}
57+
5058
/**
5159
* Our {@link hudson.model.Descriptor}
5260
*/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package jenkins.plugins.git.traits;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertNull;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
import hudson.plugins.git.extensions.impl.CloneOption;
11+
import jenkins.plugins.git.GitSCMSourceContext;
12+
13+
class CloneOptionTraitTest {
14+
15+
@Test
16+
void testShallowDisabledByDefault() {
17+
GitSCMSourceContext<?, ?> context = new GitSCMSourceContext<>(null, null);
18+
assertFalse(context.wantShallow());
19+
assertNull(context.depth());
20+
}
21+
22+
@Test
23+
void testDecorateWithDefaultCloneOption() {
24+
GitSCMSourceContext<?, ?> context = new GitSCMSourceContext<>(null, null);
25+
CloneOption cloneOption = new CloneOption(false, null, null);
26+
CloneOptionTrait cloneOptionTrait = new CloneOptionTrait(cloneOption);
27+
cloneOptionTrait.decorateContext(context);
28+
assertFalse(context.wantShallow());
29+
assertNull(context.depth());
30+
}
31+
32+
@Test
33+
void testDecorateCloneOptionWithShallow() {
34+
GitSCMSourceContext<?, ?> context = new GitSCMSourceContext<>(null, null);
35+
CloneOption cloneOption = new CloneOption(true, null, null);
36+
cloneOption.setDepth(10);
37+
CloneOptionTrait cloneOptionTrait = new CloneOptionTrait(cloneOption);
38+
cloneOptionTrait.decorateContext(context);
39+
assertTrue(context.wantShallow());
40+
assertEquals(context.depth(), 10);
41+
}
42+
}

0 commit comments

Comments
 (0)