CloneOperation implements FileCallable<Repository> { public Repository invoke(File file, VirtualChannel channel) throws IOException { String directory = repo.getDirectory(); File gitDir; if (directory == null || directory.length() == 0 || ".".equals(directory)) gitDir = file; else gitDir = new File(file, directory); CloneCommand clone = Git.cloneRepository(); clone.setURI(repo.getUri()); clone.setCloneAllBranches(false); clone.setBranchesToClone(Collections.singletonList(repo.getBranch())); clone.setDirectory(gitDir); clone.setNoCheckout(true); return clone.call().getRepository(); } CloneOperation(final BuildRepository repository); Repository invoke(File file, VirtualChannel channel);  }
@Test public void cloneRepository() throws Exception { Repository existing = git.repo(); RevCommit commit = git.add("file.txt", "abcd"); BuildRepository repo = new BuildRepository(existing.getDirectory() .toURI().toString(), "master", null); File dir = git.tempDirectory(); CloneOperation op = new CloneOperation(repo); Repository cloned = op.invoke(dir, null); assertNotNull(cloned); assertFalse(existing.getDirectory().equals(cloned.getDirectory())); assertTrue(new File(dir, Constants.DOT_GIT).exists()); assertNotNull(CommitUtils.getCommit(cloned, commit)); }
@Test public void cloneIntoSubDirectory() throws Exception { Repository existing = git.repo(); RevCommit commit = git.add("file.txt", "abcd"); BuildRepository repo = new BuildRepository(existing.getDirectory() .toURI().toString(), "master", "sub1"); CloneOperation op = new CloneOperation(repo); File dir = git.tempDirectory(); Repository cloned = op.invoke(dir, null); assertNotNull(cloned); assertFalse(existing.getDirectory().equals(cloned.getDirectory())); assertFalse(new File(dir, Constants.DOT_GIT).exists()); assertTrue(new File(dir, repo.getDirectory()).exists()); assertTrue(new File(dir, repo.getDirectory() + File.separatorChar + Constants.DOT_GIT).exists()); assertNotNull(CommitUtils.getCommit(cloned, commit)); }