Skip to content

Commit 8959d5e

Browse files
committed
[SCM-530] Add support for git submodules to git SCM provider
1 parent c4bbee3 commit 8959d5e

36 files changed

+163
-1
lines changed

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/checkout/GitCheckOutCommand.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public ScmResult executeCommand(ScmProviderRepository repo, ScmFileSet fileSet,
7070
ScmVersion version = parameters.getScmVersion(CommandParameter.SCM_VERSION, null);
7171
boolean binary = parameters.getBoolean(CommandParameter.BINARY, false);
7272
boolean shallow = parameters.getBoolean(CommandParameter.SHALLOW, false);
73+
boolean recursive = parameters.getBoolean(CommandParameter.RECURSIVE, false);
7374

7475
GitScmProviderRepository repository = (GitScmProviderRepository) repo;
7576

@@ -134,11 +135,30 @@ && new File(fileSet.getBasedir(), ".git").exists()
134135
lastCommandLine = gitCheckout.toString();
135136
}
136137

138+
if (recursive) {
139+
// and now lets do the git-submodule update
140+
Commandline clSubmoduleUpdate = createSubmoduleUpdateCommand(fileSet.getBasedir());
141+
142+
exitCode = GitCommandLineUtils.execute(clSubmoduleUpdate, stdout, stderr);
143+
if (exitCode != 0) {
144+
return new CheckOutScmResult(
145+
clSubmoduleUpdate.toString(),
146+
"The git-submodule update command failed.",
147+
stderr.getOutput(),
148+
false);
149+
}
150+
lastCommandLine = clSubmoduleUpdate.toString();
151+
}
152+
137153
// and now search for the files
138154
GitListConsumer listConsumer = new GitListConsumer(fileSet.getBasedir(), ScmFileStatus.CHECKED_IN);
139155

140156
Commandline gitList = GitListCommand.createCommandLine(repository, fileSet.getBasedir());
141157

158+
if (recursive) {
159+
gitList.createArg().setValue("--recurse-submodules");
160+
}
161+
142162
exitCode = GitCommandLineUtils.execute(gitList, listConsumer, stderr);
143163
if (exitCode != 0) {
144164
return new CheckOutScmResult(
@@ -163,6 +183,19 @@ public static Commandline createCommandLine(
163183
return gitCheckout;
164184
}
165185

186+
/**
187+
* create a git-submodule update command
188+
*/
189+
Commandline createSubmoduleUpdateCommand(File workingDirectory) {
190+
Commandline gitSubmoduleUpdate = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "submodule");
191+
192+
gitSubmoduleUpdate.createArg().setValue("update");
193+
gitSubmoduleUpdate.createArg().setValue("--init");
194+
gitSubmoduleUpdate.createArg().setValue("--recursive");
195+
196+
return gitSubmoduleUpdate;
197+
}
198+
166199
/**
167200
* create a git-clone repository command
168201
*/
@@ -183,7 +216,7 @@ private Commandline createCloneCommand(
183216
gitClone.createArg().setValue("1");
184217
}
185218

186-
if (version != null && (version instanceof ScmBranch)) {
219+
if (version instanceof ScmBranch) {
187220

188221
gitClone.createArg().setValue("--branch");
189222

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.scm.provider.git.gitexe.command.checkout;
20+
21+
import java.io.File;
22+
import java.util.Collections;
23+
24+
import org.apache.maven.scm.ScmFileSet;
25+
import org.apache.maven.scm.ScmTestCase;
26+
import org.apache.maven.scm.ScmVersion;
27+
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
28+
import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
29+
import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
30+
import org.apache.maven.scm.repository.ScmRepository;
31+
import org.codehaus.plexus.util.FileUtils;
32+
import org.codehaus.plexus.util.cli.CommandLineUtils;
33+
import org.codehaus.plexus.util.cli.Commandline;
34+
import org.junit.Before;
35+
import org.junit.Test;
36+
37+
import static org.apache.maven.scm.provider.git.GitScmTestUtils.GIT_COMMAND_LINE;
38+
import static org.junit.Assert.assertEquals;
39+
40+
/**
41+
* @author Wen Wu
42+
*
43+
*/
44+
public class GitExeCheckOutCommandRecursiveTest extends ScmTestCase {
45+
private File workingDirectory;
46+
47+
private File repo;
48+
49+
private ScmRepository scmRepository;
50+
51+
@Before
52+
@Override
53+
public void setUp() throws Exception {
54+
super.setUp();
55+
56+
workingDirectory = new File("target/checkout-recursive");
57+
FileUtils.deleteDirectory(workingDirectory);
58+
repo = new File("src/test/resources/repository_submodule");
59+
60+
scmRepository = getScmManager()
61+
.makeScmRepository(
62+
"scm:git:" + repo.toPath().toAbsolutePath().toUri().toASCIIString());
63+
}
64+
65+
@Test
66+
public void testCheckoutNoBranch() throws Exception {
67+
checkScmPresence(GIT_COMMAND_LINE);
68+
CheckOutScmResult result = checkoutRepo(false);
69+
70+
assertEquals(5, result.getCheckedOutFiles().size());
71+
72+
// git submodule set-url sub-prj file:///...
73+
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(
74+
workingDirectory,
75+
"submodule",
76+
(GitScmProviderRepository) scmRepository.getProviderRepository(),
77+
Collections.emptyMap());
78+
79+
String repoUrl = repo.toPath().toAbsolutePath().toUri().toASCIIString();
80+
cl.createArg().setValue("set-url");
81+
cl.createArg().setValue("sub-prj");
82+
cl.createArg().setValue(repoUrl);
83+
84+
CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
85+
CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
86+
GitCommandLineUtils.execute(cl, stdout, stderr);
87+
88+
result = checkoutRepo(true);
89+
assertEquals(9, result.getCheckedOutFiles().size());
90+
}
91+
92+
protected CheckOutScmResult checkoutRepo(boolean recursive) throws Exception {
93+
CheckOutScmResult result =
94+
getScmManager().checkOut(scmRepository, new ScmFileSet(workingDirectory), (ScmVersion) null, recursive);
95+
96+
assertResultIsSuccess(result);
97+
return result;
98+
}
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = true
5+
ignorecase = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unnamed repository; edit this file to name it for gitweb.

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/test/resources/repository_submodule/hooks/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# git-ls-files --others --exclude-from=.git/info/exclude
2+
# Lines that start with '#' are comments.
3+
# For a project mostly in C, the following would be a good set of
4+
# exclude patterns (uncomment them if you want to use them):
5+
# *.[oa]
6+
# *~
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0000000000000000000000000000000000000000 c51dcd33e7b71897603c203b5e0afc1d75f70051 Mark Struberg <struberg@yahoo.de> 1196106917 +0100
2+
c51dcd33e7b71897603c203b5e0afc1d75f70051 baa229a9193371fad604444f64c4f26f8ff702f3 Mark Struberg <struberg@yahoo.de> 1197041894 +0100 push
3+
baa229a9193371fad604444f64c4f26f8ff702f3 1d308c3b92eb0df9c0dc76436d50edfd0ca41d18 Mark Struberg <struberg@yahoo.de> 1197042012 +0100 push
4+
92f139dfec4d1dfb79c3cd2f94e83bf13129668b 021d26a81797f675c66ee7f875dcb4255caa6a84 wuwen <wuwen.55@aliyun.com> 1688375554 +0800 push
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0000000000000000000000000000000000000000 c51dcd33e7b71897603c203b5e0afc1d75f70051 Mark Struberg <struberg@yahoo.de> 1196106917 +0100
2+
c51dcd33e7b71897603c203b5e0afc1d75f70051 baa229a9193371fad604444f64c4f26f8ff702f3 Mark Struberg <struberg@yahoo.de> 1197041894 +0100 push
3+
baa229a9193371fad604444f64c4f26f8ff702f3 1d308c3b92eb0df9c0dc76436d50edfd0ca41d18 Mark Struberg <struberg@yahoo.de> 1197042012 +0100 push
4+
92f139dfec4d1dfb79c3cd2f94e83bf13129668b 021d26a81797f675c66ee7f875dcb4255caa6a84 wuwen <wuwen.55@aliyun.com> 1688375554 +0800 push
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0000000000000000000000000000000000000000 c51dcd33e7b71897603c203b5e0afc1d75f70051 Mark Struberg <struberg@yahoo.de> 1196106917 +0100 clone: from /home/msx/tmp/maven-git/repository/.git

0 commit comments

Comments
 (0)