Skip to content

Commit f963cc9

Browse files
committed
[SCM-530] Add support for git submodules to git SCM provider
1 parent 3d8c62b commit f963cc9

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

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

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

141157
Commandline clList = GitListCommand.createCommandLine(repository, fileSet.getBasedir());
142158

159+
if (recursive) {
160+
clList.createArg().setValue("--recurse-submodules");
161+
}
162+
143163
exitCode = GitCommandLineUtils.execute(clList, listConsumer, stderr);
144164
if (exitCode != 0) {
145165
return new CheckOutScmResult(
@@ -164,6 +184,19 @@ public static Commandline createCommandLine(
164184
return cl;
165185
}
166186

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

187-
if (version != null && (version instanceof ScmBranch)) {
220+
if (version instanceof ScmBranch) {
188221

189222
cl.createArg().setValue("--branch");
190223

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)