Skip to content

Commit bedab4f

Browse files
committed
implement decorateContext in CloneOptionTrait
1 parent fea27bd commit bedab4f

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-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: 52 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,24 @@ public final String remoteName() {
192200
return remoteName;
193201
}
194202

203+
/**
204+
* Returns {@code true} if perform shallow clone
205+
*
206+
* @return {@code true} if perform shallow clone
207+
*/
208+
public final boolean wantShallow() {
209+
return shallow;
210+
}
211+
212+
/**
213+
* Returns shallow clone depth
214+
*
215+
* @return shallow clone depth
216+
*/
217+
public final Integer depth() {
218+
return depth;
219+
}
220+
195221
/**
196222
* Adds a requirement for branch details to any {@link GitSCMSourceRequest} for this context.
197223
*
@@ -358,6 +384,32 @@ public final List<RefSpec> asRefSpecs() {
358384
return result;
359385
}
360386

387+
/**
388+
* Configures perform shallow clone
389+
*
390+
* @param shallow {@code true} to perform shallow clone
391+
* @return {@code this} for method chaining.
392+
*/
393+
@SuppressWarnings("unchecked")
394+
@NonNull
395+
public final C shallow(boolean shallow) {
396+
this.shallow = shallow;
397+
return (C) this;
398+
}
399+
400+
/**
401+
* Configures shallow clone depth
402+
*
403+
* @param depth count atest commits of a repository
404+
* @return {@code this} for method chaining.
405+
*/
406+
@SuppressWarnings("unchecked")
407+
@NonNull
408+
public final C depth(Integer depth) {
409+
this.depth = depth;
410+
return (C) this;
411+
}
412+
361413
/**
362414
* {@inheritDoc}
363415
*/

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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package jenkins.plugins.git.traits;
2+
3+
import jenkins.plugins.git.GitSCMSourceContext;
4+
import org.junit.jupiter.api.Test;
5+
6+
import hudson.plugins.git.extensions.impl.CloneOption;
7+
import jenkins.plugins.git.traits.CloneOptionTrait;
8+
9+
import static org.hamcrest.MatcherAssert.*;
10+
import static org.hamcrest.Matchers.*;
11+
12+
class CloneOptionTraitTest {
13+
14+
@Test
15+
void testShallowDisabledByDefault() {
16+
GitSCMSourceContext context = new GitSCMSourceContext(null, null);
17+
assertThat(context.wantShallow(), is(false));
18+
assertThat(context.depth(), is(null));
19+
}
20+
21+
@Test
22+
void testDecorateWithDefaultCloneOption() {
23+
GitSCMSourceContext context = new GitSCMSourceContext(null, null);
24+
CloneOption cloneOption = new CloneOption(false, null, null);
25+
CloneOptionTrait cloneOptionTrait = new CloneOptionTrait(cloneOption);
26+
cloneOptionTrait.decorateContext(context);
27+
assertThat(context.wantShallow(), is(false));
28+
assertThat(context.depth(), is(null));
29+
}
30+
31+
@Test
32+
void testDecorateCloneOptionWithShallow() {
33+
GitSCMSourceContext context = new GitSCMSourceContext(null, null);
34+
CloneOption cloneOption = new CloneOption(true, null, null);
35+
cloneOption.setDepth(10);
36+
CloneOptionTrait cloneOptionTrait = new CloneOptionTrait(cloneOption);
37+
cloneOptionTrait.decorateContext(context);
38+
assertThat(context.wantShallow(), is(true));
39+
assertThat(context.depth(), is(10));
40+
}
41+
}

0 commit comments

Comments
 (0)