Skip to content

Commit

Permalink
[JENKINS-21248] Support shallow submodule update
Browse files Browse the repository at this point in the history
  • Loading branch information
fujii committed Mar 12, 2018
1 parent 4241e81 commit 6af910a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1052,9 +1052,11 @@ public SubmoduleUpdateCommand submoduleUpdate() {
boolean recursive = false;
boolean remoteTracking = false;
boolean parentCredentials = false;
boolean shallow = false;
String ref = null;
Map<String, String> submodBranch = new HashMap<>();
public Integer timeout;
public Integer depth = 1;

public SubmoduleUpdateCommand recursive(boolean recursive) {
this.recursive = recursive;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
15 changes: 15 additions & 0 deletions src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 6af910a

Please sign in to comment.