Last active
July 21, 2023 19:15
-
-
Save rherrmann/84089f0e38d9eb875601 to your computer and use it in GitHub Desktop.
'Learning Tests' that uses the JGit API to clone existing repositories: http://www.codeaffine.com/2015/11/30/jgit-clone-repository/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codeaffine.jgit.example; | |
import static java.util.Collections.singleton; | |
import static org.eclipse.jgit.api.ListBranchCommand.ListMode.ALL; | |
import static org.eclipse.jgit.transport.RemoteConfig.getAllRemoteConfigs; | |
import static org.junit.Assert.*; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.List; | |
import org.eclipse.jgit.api.CloneCommand; | |
import org.eclipse.jgit.api.Git; | |
import org.eclipse.jgit.api.errors.GitAPIException; | |
import org.eclipse.jgit.lib.Ref; | |
import org.eclipse.jgit.transport.RemoteConfig; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.rules.TemporaryFolder; | |
public class CloneLearningTest { | |
@Rule | |
public TemporaryFolder tempFolder = new TemporaryFolder(); | |
private Git remote; | |
private Git local; | |
private CloneCommand cloneCommand; | |
@Before | |
public void setUp() throws GitAPIException, IOException { | |
remote = Git.init().setDirectory( tempFolder.newFolder( "remote" ) ).call(); | |
remote.commit().setMessage( "Initial commit" ).call(); | |
cloneCommand = Git.cloneRepository(); | |
} | |
@After | |
public void tearDown() { | |
remote.close(); | |
local.close(); | |
} | |
@Test | |
public void testClone() throws Exception { | |
File directory = getLocalRepositoryLocation(); | |
cloneCommand.setURI( getRemoteUrl() ); | |
cloneCommand.setDirectory( directory ); | |
local = cloneCommand.call(); | |
assertEquals( local.getRepository().getDirectory(), new File( directory, ".git" ) ); | |
assertEquals( local.getRepository().getWorkTree(), directory ); | |
} | |
@Test | |
public void testCloneBare() throws Exception { | |
File directory = getLocalRepositoryLocation(); | |
cloneCommand.setBare( true ); | |
cloneCommand.setURI( getRemoteUrl() ); | |
cloneCommand.setGitDir( directory ); | |
local = cloneCommand.call(); | |
assertTrue( local.getRepository().isBare() ); | |
assertEquals( local.getRepository().getDirectory(), directory ); | |
} | |
@Test | |
public void testCloneAllBranches() throws Exception { | |
remote.commit().setMessage( "initial commit" ).call(); | |
remote.branchCreate().setName( "extra" ).call(); | |
cloneCommand.setURI( getRemoteUrl() ); | |
cloneCommand.setDirectory( getLocalRepositoryLocation() ); | |
cloneCommand.setCloneAllBranches( true ); | |
local = cloneCommand.call(); | |
Ref ref = local.getRepository().getRef( "refs/remotes/origin/extra" ); | |
assertTrue( listAllLocalBranches().contains( ref ) ); | |
} | |
@Test | |
public void testCloneSpecificBranch() throws Exception { | |
remote.commit().setMessage( "initial commit" ).call(); | |
Ref extraBranch = remote.branchCreate().setName( "extra" ).call(); | |
cloneCommand.setURI( getRemoteUrl() ); | |
cloneCommand.setDirectory( getLocalRepositoryLocation() ); | |
cloneCommand.setBranchesToClone( singleton( extraBranch.getName() ) ); | |
local = cloneCommand.call(); | |
Ref ref = local.getRepository().getRef( "refs/remotes/origin/extra" ); | |
assertTrue( listAllLocalBranches().contains( ref ) ); | |
} | |
@Test | |
public void testCloneNonDefaultRemote() throws Exception { | |
cloneCommand.setURI( getRemoteUrl() ); | |
cloneCommand.setDirectory( getLocalRepositoryLocation() ); | |
cloneCommand.setRemote( "upstream" ); | |
local = cloneCommand.call(); | |
List<RemoteConfig> remoteConfigs = getAllRemoteConfigs( local.getRepository().getConfig() ); | |
assertEquals( 1, remoteConfigs.size() ); | |
assertEquals( "upstream", remoteConfigs.get( 0 ).getName() ); | |
} | |
private String getRemoteUrl() throws IOException { | |
return remote.getRepository().getDirectory().getCanonicalPath(); | |
} | |
private File getLocalRepositoryLocation() throws IOException { | |
return tempFolder.newFolder( "local" ); | |
} | |
private List<Ref> listAllLocalBranches() throws GitAPIException { | |
return local.branchList().setListMode( ALL ).call(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment