diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java index 8c8d2b08f9..70f69a4622 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java @@ -1052,9 +1052,11 @@ public SubmoduleUpdateCommand submoduleUpdate() { boolean recursive = false; boolean remoteTracking = false; boolean parentCredentials = false; + boolean shallow = false; String ref = null; Map submodBranch = new HashMap<>(); public Integer timeout; + public Integer depth = 1; public SubmoduleUpdateCommand recursive(boolean recursive) { this.recursive = recursive; @@ -1086,6 +1088,16 @@ public SubmoduleUpdateCommand timeout(Integer timeout) { return this; } + public SubmoduleUpdateCommand shallow(boolean shallow) { + this.shallow = shallow; + return this; + } + + public SubmoduleUpdateCommand depth(Integer depth) { + this.depth = depth; + return this; + } + /** * @throws GitException if executing the Git command fails * @throws InterruptedException if called methods throw same exception @@ -1116,6 +1128,12 @@ else if (!referencePath.isDirectory()) else args.add("--reference", ref); } + if (shallow) { + if (depth == null){ + depth = 1; + } + args.add("--depth=" + depth); + } // We need to call submodule update for each configured diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java index c17c2fad22..8b16f84adb 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java @@ -2114,6 +2114,7 @@ public org.jenkinsci.plugins.gitclient.SubmoduleUpdateCommand submoduleUpdate() return new org.jenkinsci.plugins.gitclient.SubmoduleUpdateCommand() { boolean recursive = false; boolean remoteTracking = false; + boolean shallow = false; String ref = null; public org.jenkinsci.plugins.gitclient.SubmoduleUpdateCommand recursive(boolean recursive) { @@ -2141,6 +2142,15 @@ public org.jenkinsci.plugins.gitclient.SubmoduleUpdateCommand timeout(Integer ti return this; } + public org.jenkinsci.plugins.gitclient.SubmoduleUpdateCommand shallow(boolean shallow) { + this.shallow = shallow; + return this; + } + + public org.jenkinsci.plugins.gitclient.SubmoduleUpdateCommand depth(Integer depth) { + return this; + } + public org.jenkinsci.plugins.gitclient.SubmoduleUpdateCommand useBranch(String submodule, String branchname) { return this; } @@ -2154,6 +2164,10 @@ public void execute() throws GitException, InterruptedException { listener.getLogger().println("[ERROR] JGit doesn't support submodule update --reference yet."); throw new UnsupportedOperationException("not implemented yet"); } + if (shallow) { + listener.getLogger().println("[ERROR] JGit doesn't support shallow submodules yet."); + throw new UnsupportedOperationException("not implemented yet"); + } try (Repository repo = getRepository()) { SubmoduleUpdateCommand update = git(repo).submoduleUpdate(); diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/SubmoduleUpdateCommand.java b/src/main/java/org/jenkinsci/plugins/gitclient/SubmoduleUpdateCommand.java index ada0c10ad6..e4f55c2df3 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/SubmoduleUpdateCommand.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/SubmoduleUpdateCommand.java @@ -58,4 +58,21 @@ public interface SubmoduleUpdateCommand extends GitCommand { * @return a {@link org.jenkinsci.plugins.gitclient.SubmoduleUpdateCommand} object. */ SubmoduleUpdateCommand timeout(Integer timeout); + + /** + * shallow. + * + * @param shallow a boolean. + * @return a {@link org.jenkinsci.plugins.gitclient.SubmoduleUpdateCommand} object. + */ + SubmoduleUpdateCommand shallow(boolean shallow); + + /** + * When shallow cloning, allow for a depth to be set in cases where you need more than the immediate last commit. + * Has no effect if shallow is set to false (default) + * + * @param depth number of revisions to be included in shallow clone + * @return a {@link org.jenkinsci.plugins.gitclient.SubmoduleUpdateCommand} object. + */ + SubmoduleUpdateCommand depth(Integer depth); } diff --git a/src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java b/src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java index 0a23542c36..a238fa79c5 100644 --- a/src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java +++ b/src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java @@ -2579,6 +2579,21 @@ public void test_submodule_update() throws Exception { assertFixSubmoduleUrlsThrows(); } + @NotImplementedInJGit + public void test_submodule_update_shallow() throws Exception { + w.init(); + w.git.clone_().url(localMirror()).repositoryName("sub2_origin").execute(); + w.git.checkout().branch("tests/getSubmodules").ref("sub2_origin/tests/getSubmodules").deleteBranchIfExist(true).execute(); + // Remove modules/sshkeys because Git can't shallow clone an unadvertised object. + // You can see the same error by invoking a following command. + // git clone --branch tests/getSubmodules --depth=1 --recurse-submodules --shallow-submodules https://github.com/jenkinsci/git-client-plugin.git + w.cmd("git rm modules/sshkeys"); + w.git.submoduleUpdate().shallow(true).execute(); + + final String shallow = ".git" + File.separator + "modules" + File.separator + "modules" + File.separator + "firewall" + File.separator + "shallow"; + assertTrue("Shallow file does not exist: " + shallow, w.exists(shallow)); + } + @NotImplementedInJGit public void test_trackingSubmoduleBranches() throws Exception { if (! ((CliGitAPIImpl)w.git).isAtLeastVersion(1,8,2,0)) {