Package org.eclipse.jgit.api
Class CloneCommand
- java.lang.Object
-
- org.eclipse.jgit.api.GitCommand<T>
-
- org.eclipse.jgit.api.TransportCommand<CloneCommand,Git>
-
- org.eclipse.jgit.api.CloneCommand
-
- All Implemented Interfaces:
java.util.concurrent.Callable<Git>
public class CloneCommand extends TransportCommand<CloneCommand,Git>
Clone a repository into a new working directory- See Also:
- Git documentation about Clone
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceCloneCommand.CallbackCallback for status of clone operation.private static classCloneCommand.FETCH_TYPE
-
Field Summary
Fields Modifier and Type Field Description private booleanbareprivate java.lang.Stringbranchprivate java.util.Collection<java.lang.String>branchesToCloneprivate CloneCommand.Callbackcallbackprivate booleancloneAllBranchesprivate booleancloneSubmodulesprivate java.io.Filedirectoryprivate booleandirectoryExistsInitiallyprivate CloneCommand.FETCH_TYPEfetchTypeprivate FSfsprivate java.io.FilegitDirprivate booleangitDirExistsInitiallyprivate booleanmirrorprivate ProgressMonitormonitorprivate booleannoCheckoutprivate java.lang.Stringremoteprivate TagOpttagOptionprivate java.lang.Stringuri-
Fields inherited from class org.eclipse.jgit.api.TransportCommand
credentialsProvider, timeout, transportConfigCallback
-
Fields inherited from class org.eclipse.jgit.api.GitCommand
repo
-
-
Constructor Summary
Constructors Constructor Description CloneCommand()Create clone command with no repository set
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaddMergeConfig(Repository clonedRepo, Ref head)private java.util.List<RefSpec>calculateRefSpecs(CloneCommand.FETCH_TYPE type, java.lang.String remoteName)Gitcall()private voidcheckout(Repository clonedRepo, FetchResult result)private voidcleanup()private voidcloneSubmodules(Repository clonedRepo)private voiddeleteChildren(java.io.File file)private FetchResultfetch(Repository clonedRepo, URIish u)private ReffindBranchToCheckout(FetchResult result)(package private) java.io.FilegetDirectory()Get the git directory.private Repositoryinit()private static booleanisNonEmptyDirectory(java.io.File dir)private RevCommitparseCommit(Repository clonedRepo, Ref ref)CloneCommandsetBare(boolean bare)Set whether the cloned repository shall be bareCloneCommandsetBranch(java.lang.String branch)Set the initial branchCloneCommandsetBranchesToClone(java.util.Collection<java.lang.String> branchesToClone)Set the branches or tags to clone.CloneCommandsetCallback(CloneCommand.Callback callback)Register a progress callback.CloneCommandsetCloneAllBranches(boolean cloneAllBranches)Set whether all branches have to be fetched.CloneCommandsetCloneSubmodules(boolean cloneSubmodules)Set whether to clone submodulesCloneCommandsetDirectory(java.io.File directory)The optional directory associated with the clone operation.private voidsetFetchType()CloneCommandsetFs(FS fs)Set the file system abstraction to be used for repositories created by this command.CloneCommandsetGitDir(java.io.File gitDir)Set the repository meta directory (.git)CloneCommandsetMirror(boolean mirror)Set up a mirror of the source repository.CloneCommandsetNoCheckout(boolean noCheckout)Set whether to skip checking out a branchCloneCommandsetNoTags()Set the --no-tags option.CloneCommandsetProgressMonitor(ProgressMonitor monitor)The progress monitor associated with the clone operation.CloneCommandsetRemote(java.lang.String remote)The remote name used to keep track of the upstream repository for the clone operation.CloneCommandsetTagOption(TagOpt tagOption)Set the tag option used for the remote configuration explicitly.CloneCommandsetURI(java.lang.String uri)Set the URI to clone fromprivate static voidvalidateDirs(java.io.File directory, java.io.File gitDir, boolean bare)(package private) voidverifyDirectories(URIish u)-
Methods inherited from class org.eclipse.jgit.api.TransportCommand
configure, configure, self, setCredentialsProvider, setTimeout, setTransportConfigCallback
-
Methods inherited from class org.eclipse.jgit.api.GitCommand
checkCallable, getRepository, setCallable
-
-
-
-
Field Detail
-
uri
private java.lang.String uri
-
directory
private java.io.File directory
-
gitDir
private java.io.File gitDir
-
bare
private boolean bare
-
fs
private FS fs
-
remote
private java.lang.String remote
-
branch
private java.lang.String branch
-
monitor
private ProgressMonitor monitor
-
cloneAllBranches
private boolean cloneAllBranches
-
mirror
private boolean mirror
-
cloneSubmodules
private boolean cloneSubmodules
-
noCheckout
private boolean noCheckout
-
branchesToClone
private java.util.Collection<java.lang.String> branchesToClone
-
callback
private CloneCommand.Callback callback
-
directoryExistsInitially
private boolean directoryExistsInitially
-
gitDirExistsInitially
private boolean gitDirExistsInitially
-
fetchType
private CloneCommand.FETCH_TYPE fetchType
-
tagOption
private TagOpt tagOption
-
-
Method Detail
-
getDirectory
@Nullable java.io.File getDirectory()
Get the git directory. This is primarily used for tests.- Returns:
- the git directory
-
call
public Git call() throws GitAPIException, InvalidRemoteException, TransportException
Execute the command
Executes the
Clonecommand. The Git instance returned by this command needs to be closed by the caller to free resources held by the underlyingRepositoryinstance. It is recommended to call this method as soon as you don't need a reference to thisGitinstance and the underlyingRepositoryinstance anymore.- Specified by:
callin interfacejava.util.concurrent.Callable<Git>- Specified by:
callin classGitCommand<Git>- Throws:
GitAPIExceptionInvalidRemoteExceptionTransportException
-
setFetchType
private void setFetchType()
-
isNonEmptyDirectory
private static boolean isNonEmptyDirectory(java.io.File dir)
-
verifyDirectories
void verifyDirectories(URIish u)
-
init
private Repository init() throws GitAPIException
- Throws:
GitAPIException
-
fetch
private FetchResult fetch(Repository clonedRepo, URIish u) throws java.net.URISyntaxException, TransportException, java.io.IOException, GitAPIException
- Throws:
java.net.URISyntaxExceptionTransportExceptionjava.io.IOExceptionGitAPIException
-
calculateRefSpecs
private java.util.List<RefSpec> calculateRefSpecs(CloneCommand.FETCH_TYPE type, java.lang.String remoteName)
-
checkout
private void checkout(Repository clonedRepo, FetchResult result) throws MissingObjectException, IncorrectObjectTypeException, java.io.IOException, GitAPIException
- Throws:
MissingObjectExceptionIncorrectObjectTypeExceptionjava.io.IOExceptionGitAPIException
-
cloneSubmodules
private void cloneSubmodules(Repository clonedRepo) throws java.io.IOException, GitAPIException
- Throws:
java.io.IOExceptionGitAPIException
-
findBranchToCheckout
private Ref findBranchToCheckout(FetchResult result)
-
addMergeConfig
private void addMergeConfig(Repository clonedRepo, Ref head) throws java.io.IOException
- Throws:
java.io.IOException
-
parseCommit
private RevCommit parseCommit(Repository clonedRepo, Ref ref) throws MissingObjectException, IncorrectObjectTypeException, java.io.IOException
- Throws:
MissingObjectExceptionIncorrectObjectTypeExceptionjava.io.IOException
-
setURI
public CloneCommand setURI(java.lang.String uri)
Set the URI to clone from- Parameters:
uri- the URI to clone from, ornullto unset the URI. The URI must be set beforecall()is called.- Returns:
- this instance
-
setDirectory
public CloneCommand setDirectory(java.io.File directory)
The optional directory associated with the clone operation. If the directory isn't set, a name associated with the source uri will be used.- Parameters:
directory- the directory to clone to, ornullif the directory name should be taken from the source uri- Returns:
- this instance
- Throws:
java.lang.IllegalStateException- if the combination of directory, gitDir and bare is illegal. E.g. if for a non-bare repository directory and gitDir point to the same directory of if for a bare repository both directory and gitDir are specified- See Also:
URIish.getHumanishName()
-
setGitDir
public CloneCommand setGitDir(java.io.File gitDir)
Set the repository meta directory (.git)- Parameters:
gitDir- the repository meta directory, ornullto choose one automatically at clone time- Returns:
- this instance
- Throws:
java.lang.IllegalStateException- if the combination of directory, gitDir and bare is illegal. E.g. if for a non-bare repository directory and gitDir point to the same directory of if for a bare repository both directory and gitDir are specified- Since:
- 3.6
-
setBare
public CloneCommand setBare(boolean bare) throws java.lang.IllegalStateException
Set whether the cloned repository shall be bare- Parameters:
bare- whether the cloned repository is bare or not- Returns:
- this instance
- Throws:
java.lang.IllegalStateException- if the combination of directory, gitDir and bare is illegal. E.g. if for a non-bare repository directory and gitDir point to the same directory of if for a bare repository both directory and gitDir are specified
-
setFs
public CloneCommand setFs(FS fs)
Set the file system abstraction to be used for repositories created by this command.- Parameters:
fs- the abstraction.- Returns:
this(for chaining calls).- Since:
- 4.10
-
setRemote
public CloneCommand setRemote(java.lang.String remote)
The remote name used to keep track of the upstream repository for the clone operation. If no remote name is set, the default value ofConstants.DEFAULT_REMOTE_NAMEwill be used.- Parameters:
remote- name that keeps track of the upstream repository.nullmeans to use DEFAULT_REMOTE_NAME.- Returns:
- this instance
- See Also:
Constants.DEFAULT_REMOTE_NAME
-
setBranch
public CloneCommand setBranch(java.lang.String branch)
Set the initial branch- Parameters:
branch- the initial branch to check out when cloning the repository. Can be specified as ref name (refs/heads/master), branch name (master) or tag name (v1.2.3). The default is to use the branch pointed to by the cloned repository's HEAD and can be requested by passingnullorHEAD.- Returns:
- this instance
-
setProgressMonitor
public CloneCommand setProgressMonitor(ProgressMonitor monitor)
The progress monitor associated with the clone operation. By default, this is set toNullProgressMonitor- Parameters:
monitor- aProgressMonitor- Returns:
this- See Also:
NullProgressMonitor
-
setCloneAllBranches
public CloneCommand setCloneAllBranches(boolean cloneAllBranches)
Set whether all branches have to be fetched.If
false, usesetBranchesToClone(Collection)to define what will be cloned. If neither are set, all branches will be cloned.- Parameters:
cloneAllBranches-truewhen all branches have to be fetched (indicates wildcard in created fetch refspec),falseotherwise.- Returns:
this
-
setMirror
public CloneCommand setMirror(boolean mirror)
Set up a mirror of the source repository. This implies that a bare repository will be created. Compared tosetBare(boolean),#setMirrornot only maps local branches of the source to local branches of the target, it maps all refs (including remote-tracking branches, notes etc.) and sets up a refspec configuration such that all these refs are overwritten by a git remote update in the target repository.- Parameters:
mirror- whether to mirror all refs from the source repository- Returns:
this- Since:
- 5.6
-
setCloneSubmodules
public CloneCommand setCloneSubmodules(boolean cloneSubmodules)
Set whether to clone submodules- Parameters:
cloneSubmodules- true to initialize and update submodules. Ignored whensetBare(boolean)is set to true.- Returns:
this
-
setBranchesToClone
public CloneCommand setBranchesToClone(java.util.Collection<java.lang.String> branchesToClone)
Set the branches or tags to clone.This is ignored if
setCloneAllBranches(true)orsetMirror(true)is used. IfbranchesToCloneisnullor empty, it's also ignored.- Parameters:
branchesToClone- collection of branches to clone. Must be specified as full ref names (e.g.refs/heads/masterorrefs/tags/v1.0.0).- Returns:
this
-
setTagOption
public CloneCommand setTagOption(TagOpt tagOption)
Set the tag option used for the remote configuration explicitly.- Parameters:
tagOption- tag option to be used for the remote config- Returns:
this- Since:
- 5.8
-
setNoTags
public CloneCommand setNoTags()
Set the --no-tags option. Tags are not cloned now and the remote configuration is initialized with the --no-tags option as well.- Returns:
this- Since:
- 5.8
-
setNoCheckout
public CloneCommand setNoCheckout(boolean noCheckout)
Set whether to skip checking out a branch- Parameters:
noCheckout- if set totrueno branch will be checked out after the clone. This enhances performance of the clone command when there is no need for a checked out branch.- Returns:
this
-
setCallback
public CloneCommand setCallback(CloneCommand.Callback callback)
Register a progress callback.- Parameters:
callback- the callback- Returns:
this- Since:
- 4.8
-
validateDirs
private static void validateDirs(java.io.File directory, java.io.File gitDir, boolean bare) throws java.lang.IllegalStateException- Throws:
java.lang.IllegalStateException
-
cleanup
private void cleanup()
-
deleteChildren
private void deleteChildren(java.io.File file) throws java.io.IOException- Throws:
java.io.IOException
-
-