Skip to content

Commit cf566f1

Browse files
author
Andrew Bayer
committed
[HUDSON-6258] Added support for --recursive calls to submodule commands
1 parent d89e7f4 commit cf566f1

File tree

6 files changed

+70
-18
lines changed

6 files changed

+70
-18
lines changed

src/main/java/hudson/plugins/git/GitAPI.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,36 @@ public void submoduleSync() throws GitException {
296296
/**
297297
* Update submodules.
298298
*
299+
* @param recursive if true, will recursively update submodules (requires git>=1.6.5)
300+
*
299301
* @throws GitException if executing the Git command fails
300302
*/
301-
public void submoduleUpdate() throws GitException {
302-
launchCommand("submodule", "update");
303+
public void submoduleUpdate(boolean recursive) throws GitException {
304+
ArgumentListBuilder args = new ArgumentListBuilder();
305+
args.add("submodule", "update");
306+
if (recursive) {
307+
args.add("--init", "--recursive");
308+
}
309+
310+
launchCommand(args);
311+
}
312+
313+
/**
314+
* Cleans submodules
315+
*
316+
* @param recursive if true, will recursively clean submodules (requres git>=1.6.5)
317+
*
318+
* @throws GitException if executing the git command fails
319+
*/
320+
public void submoduleClean(boolean recursive) throws GitException {
321+
ArgumentListBuilder args = new ArgumentListBuilder();
322+
args.add("submodule", "foreach");
323+
if (recursive) {
324+
args.add("--recursive");
325+
}
326+
args.add("git clean -fdx");
327+
328+
launchCommand(args);
303329
}
304330

305331
public void tag(String tagName, String comment) throws GitException {

src/main/java/hudson/plugins/git/GitSCM.java

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ public class GitSCM extends SCM implements Serializable {
9090
*/
9191
private PreBuildMergeOptions mergeOptions;
9292

93+
/**
94+
* Use --recursive flag on submodule commands - requires git>=1.6.5
95+
*/
96+
private boolean recursiveSubmodules;
97+
9398
private boolean doGenerateSubmoduleConfigurations;
9499
private boolean authorOrCommitter;
95100

@@ -143,7 +148,8 @@ public GitSCM(
143148
String relativeTargetDir,
144149
String excludedRegions,
145150
String excludedUsers,
146-
String localBranch) {
151+
String localBranch,
152+
boolean recursiveSubmodules) {
147153

148154
// normalization
149155
this.branches = branches;
@@ -166,6 +172,7 @@ public GitSCM(
166172
this.relativeTargetDir = relativeTargetDir;
167173
this.excludedRegions = excludedRegions;
168174
this.excludedUsers = excludedUsers;
175+
this.recursiveSubmodules = recursiveSubmodules;
169176
buildChooser.gitSCM = this; // set the owner
170177
}
171178

@@ -183,7 +190,8 @@ public Object readResolve() {
183190
doGenerateSubmoduleConfigurations = false;
184191
mergeOptions = new PreBuildMergeOptions();
185192

186-
193+
recursiveSubmodules = false;
194+
187195
remoteRepositories.add(newRemoteConfig("origin", source, new RefSpec("+refs/heads/*:refs/remotes/origin/*")));
188196
if(branch != null) {
189197
branches.add(new BranchSpec(branch));
@@ -779,7 +787,7 @@ public Revision invoke(File localWorkspace, VirtualChannel channel)
779787

780788
if (git.hasGitModules()) {
781789
git.submoduleInit();
782-
git.submoduleUpdate();
790+
git.submoduleUpdate(recursiveSubmodules);
783791
}
784792
}
785793

@@ -857,16 +865,17 @@ public Object[] invoke(File localWorkspace, VirtualChannel channel)
857865
if (getClean()) {
858866
listener.getLogger().println("Cleaning workspace");
859867
git.clean();
860-
for (RemoteConfig remoteRepository : paramRepos) {
861-
cleanSubmodules(git,localWorkspace,listener,remoteRepository);
868+
869+
if (git.hasGitModules()) {
870+
git.submoduleClean(recursiveSubmodules);
862871
}
863872
}
864873

865874
return new Object[]{null, buildData};
866875
}
867876

868877
if (git.hasGitModules()) {
869-
git.submoduleUpdate();
878+
git.submoduleUpdate(recursiveSubmodules);
870879
}
871880

872881
// Tag the successful merge
@@ -897,8 +906,8 @@ public Object[] invoke(File localWorkspace, VirtualChannel channel)
897906
if (getClean()) {
898907
listener.getLogger().println("Cleaning workspace");
899908
git.clean();
900-
for (RemoteConfig remoteRepository : paramRepos) {
901-
cleanSubmodules(git,localWorkspace,listener,remoteRepository);
909+
if (git.hasGitModules()) {
910+
git.submoduleClean(recursiveSubmodules);
902911
}
903912
}
904913

@@ -948,13 +957,17 @@ public Object[] invoke(File localWorkspace, VirtualChannel channel)
948957
// we've only just discovered.
949958
// So - try updating from all RRs, then use the submodule
950959
// Update to do the checkout
951-
952-
for (RemoteConfig remoteRepository : paramRepos) {
953-
fetchFrom(git, localWorkspace, listener, remoteRepository);
960+
//
961+
// Also, only do this if we're not doing recursive submodules, since that'll
962+
// theoretically be dealt with there anyway.
963+
if (!recursiveSubmodules) {
964+
for (RemoteConfig remoteRepository : paramRepos) {
965+
fetchFrom(git, localWorkspace, listener, remoteRepository);
966+
}
954967
}
955968

956969
// Update to the correct checkout
957-
git.submoduleUpdate();
970+
git.submoduleUpdate(recursiveSubmodules);
958971

959972
}
960973

@@ -1125,7 +1138,8 @@ public SCM newInstance(StaplerRequest req, JSONObject formData) throws FormExcep
11251138
req.getParameter("git.relativeTargetDir"),
11261139
req.getParameter("git.excludedRegions"),
11271140
req.getParameter("git.excludedUsers"),
1128-
req.getParameter("git.localBranch"));
1141+
req.getParameter("git.localBranch"),
1142+
req.getParameter("git.recursiveSubmodules") != null);
11291143
}
11301144

11311145
/**
@@ -1279,7 +1293,10 @@ public FormValidation doGitRemoteNameCheck(StaplerRequest req, StaplerResponse r
12791293
private static final long serialVersionUID = 1L;
12801294

12811295

1282-
1296+
public boolean getRecursiveSubmodules() {
1297+
return this.recursiveSubmodules;
1298+
}
1299+
12831300
public boolean getDoGenerate() {
12841301
return this.doGenerateSubmoduleConfigurations;
12851302
}

src/main/java/hudson/plugins/git/IGitAPI.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public interface IGitAPI {
2121
boolean hasGitModules() throws GitException;
2222

2323
void submoduleInit() throws GitException;
24-
void submoduleUpdate() throws GitException;
24+
void submoduleUpdate(boolean recursive) throws GitException;
25+
void submoduleClean(boolean recursive) throws GitException;
2526
void submoduleSync() throws GitException;
2627

2728
public void fetch(String repository, String refspec) throws GitException;

src/main/resources/hudson/plugins/git/GitSCM/config.jelly

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@
132132
<f:entry title="Clean after checkout" help="/plugin/git/clean.html">
133133
<f:checkbox name="git.clean" checked="${scm.clean}" />
134134
</f:entry>
135+
<f:entry title="Recursively update submodules" help="/plugin/git/help-recursiveSubmodules.html">
136+
<f:checkbox name="git.recursiveSubmodules" checked="${scm.recursiveSubmodules}" />
137+
</f:entry>
135138
<f:entry title="Use commit author in changelog" help="/plugin/git/help-authorCommitter.html">
136139
<f:checkbox name="git.authorOrCommitter" checked="${scm.authorOrCommitter}"/>
137140
</f:entry>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div>
2+
Retrieve all submodules recursively
3+
4+
(uses '--recursive' option which requires git>=1.6.5)
5+
</div>

src/test/java/hudson/plugins/git/GitSCMTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ private FreeStyleProject setupProject(String branchString, boolean authorOrCommi
391391
Collections.singletonList(new BranchSpec(branchString)),
392392
new PreBuildMergeOptions(), false, Collections.<SubmoduleConfig>emptyList(), false,
393393
false, new DefaultBuildChooser(), null, null, authorOrCommitter, relativeTargetDir,
394-
excludedRegions, excludedUsers, localBranch));
394+
excludedRegions, excludedUsers, localBranch, false));
395395
project.getBuildersList().add(new CaptureEnvironmentBuilder());
396396
return project;
397397
}

0 commit comments

Comments
 (0)