From 89ba2d3ff38ec5bc870441ec06b73fbfcab6743e Mon Sep 17 00:00:00 2001 From: Fujii Hironori Date: Thu, 8 Mar 2018 17:42:35 -0800 Subject: [PATCH] [JENKINS-21248] Support shallow submodule update --- .../plugins/gitclient/CliGitAPIImpl.java | 18 ++++++++++++++++++ .../plugins/gitclient/JGitAPIImpl.java | 14 ++++++++++++++ .../gitclient/SubmoduleUpdateCommand.java | 17 +++++++++++++++++ .../plugins/gitclient/GitAPITestCase.java | 12 ++++++++++++ 4 files changed, 61 insertions(+) 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 0e9f7e7ad9..cdf4806704 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java @@ -2101,6 +2101,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) { @@ -2128,6 +2129,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; } @@ -2141,6 +2151,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 2aef109b3b..bc651a383a 100644 --- a/src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java +++ b/src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java @@ -2570,6 +2570,18 @@ 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/getRelativeSubmodule").ref("sub2_origin/tests/getRelativeSubmodule").deleteBranchIfExist(true).execute(); + w.git.submoduleInit(); + w.git.submoduleUpdate().shallow(true).execute(); + + final String shallow = ".git" + File.separator + "modules" + File.separator + "sub" + 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)) {