Skip to content

Commit

Permalink
[SCM-530] Add support for git submodules to git SCM provider
Browse files Browse the repository at this point in the history
  • Loading branch information
wuwen5 committed Dec 16, 2024
1 parent ad63ac5 commit 8eb121b
Show file tree
Hide file tree
Showing 36 changed files with 163 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public ScmResult executeCommand(ScmProviderRepository repo, ScmFileSet fileSet,
ScmVersion version = parameters.getScmVersion(CommandParameter.SCM_VERSION, null);
boolean binary = parameters.getBoolean(CommandParameter.BINARY, false);
boolean shallow = parameters.getBoolean(CommandParameter.SHALLOW, false);
boolean recursive = parameters.getBoolean(CommandParameter.RECURSIVE, false);

GitScmProviderRepository repository = (GitScmProviderRepository) repo;

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

if (recursive) {
// and now lets do the git-submodule update
Commandline clSubmoduleUpdate = createSubmoduleUpdateCommand(fileSet.getBasedir());

exitCode = GitCommandLineUtils.execute(clSubmoduleUpdate, stdout, stderr);
if (exitCode != 0) {
return new CheckOutScmResult(
clSubmoduleUpdate.toString(),
"The git-submodule update command failed.",
stderr.getOutput(),
false);
}
lastCommandLine = clSubmoduleUpdate.toString();
}

// and now search for the files
GitListConsumer listConsumer = new GitListConsumer(fileSet.getBasedir(), ScmFileStatus.CHECKED_IN);

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

if (recursive) {
gitList.createArg().setValue("--recurse-submodules");
}

exitCode = GitCommandLineUtils.execute(gitList, listConsumer, stderr);
if (exitCode != 0) {
return new CheckOutScmResult(
Expand All @@ -163,6 +183,19 @@ public static Commandline createCommandLine(
return gitCheckout;
}

/**
* create a git-submodule update command
*/
Commandline createSubmoduleUpdateCommand(File workingDirectory) {
Commandline gitSubmoduleUpdate = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "submodule");

gitSubmoduleUpdate.createArg().setValue("update");
gitSubmoduleUpdate.createArg().setValue("--init");
gitSubmoduleUpdate.createArg().setValue("--recursive");

return gitSubmoduleUpdate;
}

/**
* create a git-clone repository command
*/
Expand All @@ -183,7 +216,7 @@ private Commandline createCloneCommand(
gitClone.createArg().setValue("1");
}

if (version != null && (version instanceof ScmBranch)) {
if (version instanceof ScmBranch) {

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.scm.provider.git.gitexe.command.checkout;

import java.io.File;
import java.util.Collections;

import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.ScmTestCase;
import org.apache.maven.scm.ScmVersion;
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
import org.apache.maven.scm.repository.ScmRepository;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.cli.Commandline;
import org.junit.Before;
import org.junit.Test;

import static org.apache.maven.scm.provider.git.GitScmTestUtils.GIT_COMMAND_LINE;
import static org.junit.Assert.assertEquals;

/**
* @author Wen Wu
*
*/
public class GitExeCheckOutCommandRecursiveTest extends ScmTestCase {
private File workingDirectory;

private File repo;

private ScmRepository scmRepository;

@Before
@Override
public void setUp() throws Exception {
super.setUp();

workingDirectory = new File("target/checkout-recursive");
FileUtils.deleteDirectory(workingDirectory);
repo = new File("src/test/resources/repository_submodule");

scmRepository = getScmManager()
.makeScmRepository(
"scm:git:" + repo.toPath().toAbsolutePath().toUri().toASCIIString());
}

@Test
public void testCheckoutNoBranch() throws Exception {
checkScmPresence(GIT_COMMAND_LINE);
CheckOutScmResult result = checkoutRepo(false);

assertEquals(5, result.getCheckedOutFiles().size());

// git submodule set-url sub-prj file:///...
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(
workingDirectory,
"submodule",
(GitScmProviderRepository) scmRepository.getProviderRepository(),
Collections.emptyMap());

String repoUrl = repo.toPath().toAbsolutePath().toUri().toASCIIString();
cl.createArg().setValue("set-url");
cl.createArg().setValue("sub-prj");
cl.createArg().setValue(repoUrl);

CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
GitCommandLineUtils.execute(cl, stdout, stderr);

result = checkoutRepo(true);
assertEquals(9, result.getCheckedOutFiles().size());
}

protected CheckOutScmResult checkoutRepo(boolean recursive) throws Exception {
CheckOutScmResult result =
getScmManager().checkOut(scmRepository, new ScmFileSet(workingDirectory), (ScmVersion) null, recursive);

assertResultIsSuccess(result);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[core]
repositoryformatversion = 0
filemode = true
bare = true
ignorecase = true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file to name it for gitweb.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# git-ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0000000000000000000000000000000000000000 c51dcd33e7b71897603c203b5e0afc1d75f70051 Mark Struberg <[email protected]> 1196106917 +0100
c51dcd33e7b71897603c203b5e0afc1d75f70051 baa229a9193371fad604444f64c4f26f8ff702f3 Mark Struberg <[email protected]> 1197041894 +0100 push
baa229a9193371fad604444f64c4f26f8ff702f3 1d308c3b92eb0df9c0dc76436d50edfd0ca41d18 Mark Struberg <[email protected]> 1197042012 +0100 push
92f139dfec4d1dfb79c3cd2f94e83bf13129668b 021d26a81797f675c66ee7f875dcb4255caa6a84 wuwen <[email protected]> 1688375554 +0800 push
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0000000000000000000000000000000000000000 c51dcd33e7b71897603c203b5e0afc1d75f70051 Mark Struberg <[email protected]> 1196106917 +0100
c51dcd33e7b71897603c203b5e0afc1d75f70051 baa229a9193371fad604444f64c4f26f8ff702f3 Mark Struberg <[email protected]> 1197041894 +0100 push
baa229a9193371fad604444f64c4f26f8ff702f3 1d308c3b92eb0df9c0dc76436d50edfd0ca41d18 Mark Struberg <[email protected]> 1197042012 +0100 push
92f139dfec4d1dfb79c3cd2f94e83bf13129668b 021d26a81797f675c66ee7f875dcb4255caa6a84 wuwen <[email protected]> 1688375554 +0800 push
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0000000000000000000000000000000000000000 c51dcd33e7b71897603c203b5e0afc1d75f70051 Mark Struberg <[email protected]> 1196106917 +0100 clone: from /home/msx/tmp/maven-git/repository/.git
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x��A
� E��� A�hJ�Uƙ�bRR%���AW�����ֹigå�":�M���� b#��G��0�xꅻ�M'W,$.B#[.yJĮ�Q"��Y�B�Yao�m�G?dշ� �?p�?}h�wmC�0yI_M4F��<���L!�~�\7/��ED
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xm���0 DYۯ��Gba�3�C�ZmPZG�Ӗ�'�]�޽ӝO���z9=������tm�W�l��t47�5mS8�wAXˊ3P�Y�p@pمar �V�tP��%�����Q��]ㆠ�T��VQ3IT�w�_RE�
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x��1�0 @Q��;��� !.�� \ǡ�A��=�ly�R��@k����1H�������p�Q(I ��5q��W�j� �;\����_�:�y�u����H=}[$D'ߥ�_ؕ�X���Y����<m
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x��MN1 @a�9����O���XqDZ��ʤ nB����<�~��|��9T�I�K��������ENV��� F��w��IԤ���f*K>b��&E6���e�D������x��9�U����:}����/@�d�TJ���_��͎[��~‡�}����Ƕ���}���jP�
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
021d26a81797f675c66ee7f875dcb4255caa6a84
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/remotes/origin/master
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c51dcd33e7b71897603c203b5e0afc1d75f70051

0 comments on commit 8eb121b

Please sign in to comment.