Add support for cloning submodules to CloneCommand
Change-Id: Ib1f82206fa14be75e2080f95db9f2d178e876453
This commit is contained in:
parent
dea4511572
commit
d57c00e036
|
@ -51,6 +51,7 @@
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
|
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
|
||||||
import org.eclipse.jgit.api.errors.JGitInternalException;
|
import org.eclipse.jgit.api.errors.JGitInternalException;
|
||||||
|
@ -63,6 +64,9 @@
|
||||||
import org.eclipse.jgit.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
import org.eclipse.jgit.lib.RepositoryTestCase;
|
import org.eclipse.jgit.lib.RepositoryTestCase;
|
||||||
import org.eclipse.jgit.revwalk.RevBlob;
|
import org.eclipse.jgit.revwalk.RevBlob;
|
||||||
|
import org.eclipse.jgit.revwalk.RevCommit;
|
||||||
|
import org.eclipse.jgit.submodule.SubmoduleStatus;
|
||||||
|
import org.eclipse.jgit.submodule.SubmoduleStatusType;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class CloneCommandTest extends RepositoryTestCase {
|
public class CloneCommandTest extends RepositoryTestCase {
|
||||||
|
@ -252,4 +256,47 @@ public void testCloneRepositoryWithMultipleHeadBranches() throws Exception {
|
||||||
|
|
||||||
assertEquals(Constants.MASTER, git2.getRepository().getBranch());
|
assertEquals(Constants.MASTER, git2.getRepository().getBranch());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCloneRepositoryWithSubmodules() throws Exception {
|
||||||
|
git.checkout().setName(Constants.MASTER).call();
|
||||||
|
|
||||||
|
String file = "file.txt";
|
||||||
|
writeTrashFile(file, "content");
|
||||||
|
git.add().addFilepattern(file).call();
|
||||||
|
RevCommit commit = git.commit().setMessage("create file").call();
|
||||||
|
|
||||||
|
SubmoduleAddCommand command = new SubmoduleAddCommand(db);
|
||||||
|
String path = "sub";
|
||||||
|
command.setPath(path);
|
||||||
|
String uri = db.getDirectory().toURI().toString();
|
||||||
|
command.setURI(uri);
|
||||||
|
Repository repo = command.call();
|
||||||
|
assertNotNull(repo);
|
||||||
|
git.add().addFilepattern(path)
|
||||||
|
.addFilepattern(Constants.DOT_GIT_MODULES).call();
|
||||||
|
git.commit().setMessage("adding submodule").call();
|
||||||
|
|
||||||
|
File directory = createTempDirectory("testCloneRepositoryWithSubmodules");
|
||||||
|
CloneCommand clone = Git.cloneRepository();
|
||||||
|
clone.setDirectory(directory);
|
||||||
|
clone.setCloneSubmodules(true);
|
||||||
|
clone.setURI("file://" + git.getRepository().getWorkTree().getPath());
|
||||||
|
Git git2 = clone.call();
|
||||||
|
addRepoToClose(git2.getRepository());
|
||||||
|
assertNotNull(git2);
|
||||||
|
|
||||||
|
assertEquals(Constants.MASTER, git2.getRepository().getBranch());
|
||||||
|
assertTrue(new File(git2.getRepository().getWorkTree(), path
|
||||||
|
+ File.separatorChar + file).exists());
|
||||||
|
|
||||||
|
SubmoduleStatusCommand status = new SubmoduleStatusCommand(
|
||||||
|
git2.getRepository());
|
||||||
|
Map<String, SubmoduleStatus> statuses = status.call();
|
||||||
|
SubmoduleStatus pathStatus = statuses.get(path);
|
||||||
|
assertNotNull(pathStatus);
|
||||||
|
assertEquals(SubmoduleStatusType.INITIALIZED, pathStatus.getType());
|
||||||
|
assertEquals(commit, pathStatus.getHeadId());
|
||||||
|
assertEquals(commit, pathStatus.getIndexId());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,6 +94,8 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
|
||||||
|
|
||||||
private boolean cloneAllBranches;
|
private boolean cloneAllBranches;
|
||||||
|
|
||||||
|
private boolean cloneSubmodules;
|
||||||
|
|
||||||
private boolean noCheckout;
|
private boolean noCheckout;
|
||||||
|
|
||||||
private Collection<String> branchesToClone;
|
private Collection<String> branchesToClone;
|
||||||
|
@ -222,9 +224,22 @@ private void checkout(Repository clonedRepo, FetchResult result)
|
||||||
DirCacheCheckout co = new DirCacheCheckout(clonedRepo, dc,
|
DirCacheCheckout co = new DirCacheCheckout(clonedRepo, dc,
|
||||||
commit.getTree());
|
commit.getTree());
|
||||||
co.checkout();
|
co.checkout();
|
||||||
|
if (cloneSubmodules)
|
||||||
|
cloneSubmodules(clonedRepo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void cloneSubmodules(Repository clonedRepo) {
|
||||||
|
SubmoduleInitCommand init = new SubmoduleInitCommand(clonedRepo);
|
||||||
|
if (init.call().isEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
SubmoduleUpdateCommand update = new SubmoduleUpdateCommand(clonedRepo);
|
||||||
|
configure(update);
|
||||||
|
update.setProgressMonitor(monitor);
|
||||||
|
update.call();
|
||||||
|
}
|
||||||
|
|
||||||
private Ref findBranchToCheckout(FetchResult result) {
|
private Ref findBranchToCheckout(FetchResult result) {
|
||||||
final Ref idHEAD = result.getAdvertisedRef(Constants.HEAD);
|
final Ref idHEAD = result.getAdvertisedRef(Constants.HEAD);
|
||||||
if (idHEAD == null)
|
if (idHEAD == null)
|
||||||
|
@ -356,6 +371,17 @@ public CloneCommand setCloneAllBranches(boolean cloneAllBranches) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param cloneSubmodules
|
||||||
|
* true to initialize and update submodules. Ignored when
|
||||||
|
* {@link #setBare(boolean)} is set to true.
|
||||||
|
* @return {@code this}
|
||||||
|
*/
|
||||||
|
public CloneCommand setCloneSubmodules(boolean cloneSubmodules) {
|
||||||
|
this.cloneSubmodules = cloneSubmodules;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param branchesToClone
|
* @param branchesToClone
|
||||||
* collection of branches to clone. Ignored when allSelected is
|
* collection of branches to clone. Ignored when allSelected is
|
||||||
|
|
Loading…
Reference in New Issue