Top Description Inners Fields Constructors Methods
org.eclipse.jgit.api

public Class CloneCommand

extends TransportCommand<CloneCommand, Git>
Class Inheritance
Imports
java.io.File, .IOException, java.net.URISyntaxException, java.text.MessageFormat, java.time.Instant, .OffsetDateTime, java.util.ArrayList, .Collection, .List, org.eclipse.jgit.annotations.NonNull, .Nullable, org.eclipse.jgit.api.errors.GitAPIException, .InvalidRemoteException, .JGitInternalException, org.eclipse.jgit.dircache.DirCache, .DirCacheCheckout, org.eclipse.jgit.errors.IncorrectObjectTypeException, .MissingObjectException, org.eclipse.jgit.internal.JGitText, org.eclipse.jgit.internal.util.ShutdownHook, org.eclipse.jgit.lib.AnyObjectId, .BranchConfig.BranchRebaseMode, .ConfigConstants, .Constants, .NullProgressMonitor, .ObjectId, .ProgressMonitor, .Ref, .RefUpdate, .Repository, org.eclipse.jgit.revwalk.RevCommit, .RevWalk, org.eclipse.jgit.submodule.SubmoduleWalk, org.eclipse.jgit.transport.FetchResult, .RefSpec, .RemoteConfig, .TagOpt, .URIish, org.eclipse.jgit.util.FS, .FileUtils

Clone a repository into a new working directory
See Also
Git documentation about Clone

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
CloneCommand.Callback

Callback for status of clone operation.

private static enum

Field Summary

Modifier and TypeField and Description
private boolean
private String
private Collection<String>
private CloneCommand.Callback
private boolean
private boolean
private Integer
private File
private boolean
private CloneCommand.FETCH_TYPE
private FS
private File
private boolean
private boolean
private ProgressMonitor
private boolean
private boolean
private String
private List<String>
private Instant
private ShutdownHook.Listener
private TagOpt
private String
Inherited from org.eclipse.jgit.api.TransportCommand:
credentialsProvidertimeouttransportConfigCallback

Constructor Summary

AccessConstructor and Description
public
CloneCommand()

Create clone command with no repository set

Method Summary

Modifier and TypeMethod and Description
private void
addMergeConfig(Repository clonedRepo, Ref head)

public CloneCommand

Returns:

this
addShallowExclude
(String
the ref or commit; must not be null
shallowExclude
)

Creates a shallow clone with a history, excluding commits reachable from a specified remote branch or tag.

public CloneCommand

Returns:

this
addShallowExclude
(ObjectId
the commit; must not be null
shallowExclude
)

Creates a shallow clone with a history, excluding commits reachable from a specified remote branch or tag.

private List<RefSpec>
public Git
call()

Implements abstract org.eclipse.jgit.api.GitCommand.call.

Implements java.util.concurrent.Callable.call.

Computes a result, or throws an exception if unable to do so.

private void
checkout(Repository clonedRepo, FetchResult result)

private void
private void
private void
private FetchResult
fetch(Repository clonedRepo, URIish u)

private Ref
pack-priv File

Returns:

the git directory
getDirectory
()

Get the git directory.

private Repository
init()

private static boolean
private RevCommit
parseCommit(Repository clonedRepo, Ref ref)

public CloneCommand

Returns:

this instance
setBare
(boolean
whether the cloned repository is bare or not
bare
)

Set whether the cloned repository shall be bare

public CloneCommand

Returns:

this instance
setBranch
(String
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 passing null or HEAD.
branch
)

Set the initial branch

public CloneCommand

Returns:

this
setBranchesToClone
(Collection<String>
collection of branches to clone. Must be specified as full ref names (e.g. refs/heads/master or refs/tags/v1.0.0).
branchesToClone
)

Set the branches or tags to clone.

public CloneCommand

Returns:

this
setCallback
(CloneCommand.Callback
the callback
callback
)

Register a progress callback.

public CloneCommand

Returns:

this
setCloneAllBranches
(boolean
true when all branches have to be fetched (indicates wildcard in created fetch refspec), false otherwise.
cloneAllBranches
)

Set whether all branches have to be fetched.

public CloneCommand

Returns:

this
setCloneSubmodules
(boolean
true to initialize and update submodules. Ignored when setBare(boolean) is set to true.
cloneSubmodules
)

Set whether to clone submodules

public CloneCommand

Returns:

this
setDepth
(int
the depth
depth
)

Creates a shallow clone with a history truncated to the specified number of commits.

public CloneCommand

Returns:

this instance
setDirectory
(File
the directory to clone to, or null if the directory name should be taken from the source uri
directory
)

The optional directory associated with the clone operation.

private void
public CloneCommand

Returns:

this (for chaining calls).
setFs
(FS
the abstraction.
fs
)

Set the file system abstraction to be used for repositories created by this command.

public CloneCommand

Returns:

this instance
setGitDir
(File
the repository meta directory, or null to choose one automatically at clone time
gitDir
)

Set the repository meta directory (.git)

public CloneCommand

Returns:

this
setMirror
(boolean
whether to mirror all refs from the source repository
mirror
)

Set up a mirror of the source repository.

public CloneCommand

Returns:

this
setNoCheckout
(boolean
if set to true no 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.
noCheckout
)

Set whether to skip checking out a branch

public CloneCommand

Returns:

this
setNoTags
()

Set the --no-tags option.

public CloneCommand

Returns:

this
setProgressMonitor
(ProgressMonitor monitor)

The progress monitor associated with the clone operation.

public CloneCommand

Returns:

this instance
setRelativePaths
(boolean
if true, use relative paths for GIT_DIR and GIT_WORK_TREE
relativePaths
)

Set whether the cloned repository shall use relative paths for GIT_DIR and GIT_WORK_TREE

public CloneCommand

Returns:

this instance
setRemote
(String
name that keeps track of the upstream repository. null means to use DEFAULT_REMOTE_NAME.
remote
)

The remote name used to keep track of the upstream repository for the clone operation.

public CloneCommand

Returns:

this
setShallowSince
(OffsetDateTime
the timestammp; must not be null
shallowSince
)

Creates a shallow clone with a history after the specified time.

public CloneCommand

Returns:

this
setShallowSince
(Instant
the timestammp; must not be null
shallowSince
)

Creates a shallow clone with a history after the specified time.

public CloneCommand

Returns:

this
setTagOption
(TagOpt
tag option to be used for the remote config
tagOption
)

Set the tag option used for the remote configuration explicitly.

public CloneCommand

Returns:

this instance
setURI
(String
the URI to clone from, or null to unset the URI. The URI must be set before call is called.
uri
)

Set the URI to clone from

private static void
validateDirs(File directory, File gitDir, boolean bare)

pack-priv void
Inherited from org.eclipse.jgit.api.TransportCommand:
configureconfigureselfsetCredentialsProvidersetTimeoutsetTransportConfigCallback

Field Detail

bareback to summary
private boolean bare
branchback to summary
private String branch
branchesToCloneback to summary
private Collection<String> branchesToClone
callbackback to summary
private CloneCommand.Callback callback
cloneAllBranchesback to summary
private boolean cloneAllBranches
cloneSubmodulesback to summary
private boolean cloneSubmodules
depthback to summary
private Integer depth
directoryback to summary
private File directory
directoryExistsInitiallyback to summary
private boolean directoryExistsInitially
fetchTypeback to summary
private CloneCommand.FETCH_TYPE fetchType
fsback to summary
private FS fs
gitDirback to summary
private File gitDir
gitDirExistsInitiallyback to summary
private boolean gitDirExistsInitially
mirrorback to summary
private boolean mirror
monitorback to summary
private ProgressMonitor monitor
noCheckoutback to summary
private boolean noCheckout
relativePathsback to summary
private boolean relativePaths
remoteback to summary
private String remote
shallowExcludesback to summary
private List<String> shallowExcludes
shallowSinceback to summary
private Instant shallowSince
shutdownListenerback to summary
private ShutdownHook.Listener shutdownListener
tagOptionback to summary
private TagOpt tagOption
uriback to summary
private String uri

Constructor Detail

CloneCommandback to summary
public CloneCommand()

Create clone command with no repository set

Method Detail

addMergeConfigback to summary
private void addMergeConfig(Repository clonedRepo, Ref head) throws IOException
addShallowExcludeback to summary
public CloneCommand addShallowExclude(String shallowExclude)

Creates a shallow clone with a history, excluding commits reachable from a specified remote branch or tag.

Parameters
shallowExclude:String

the ref or commit; must not be null

Returns:CloneCommand

this

Since
6.3
addShallowExcludeback to summary
public CloneCommand addShallowExclude(ObjectId shallowExclude)

Creates a shallow clone with a history, excluding commits reachable from a specified remote branch or tag.

Parameters
shallowExclude:ObjectId

the commit; must not be null

Returns:CloneCommand

this

Since
6.3
calculateRefSpecsback to summary
private List<RefSpec> calculateRefSpecs(CloneCommand.FETCH_TYPE type, String remoteName)
callback to summary
public Git call() throws GitAPIException, InvalidRemoteException, TransportException

Implements abstract org.eclipse.jgit.api.GitCommand.call.

Implements java.util.concurrent.Callable.call.

Doc from org.eclipse.jgit.api.GitCommand.call.

Computes a result, or throws an exception if unable to do so.

Execute the command

Executes the Clone command. The Git instance returned by this command needs to be closed by the caller to free resources held by the underlying Repository instance. It is recommended to call this method as soon as you don't need a reference to this Git instance and the underlying Repository instance anymore.

Returns:Git

Doc from java.util.concurrent.Callable.call.

computed result

Annotations
@Override
checkoutback to summary
private void checkout(Repository clonedRepo, FetchResult result) throws MissingObjectException, IncorrectObjectTypeException, IOException, GitAPIException
cleanupback to summary
private void cleanup()
cloneSubmodulesback to summary
private void cloneSubmodules(Repository clonedRepo) throws IOException, GitAPIException
deleteChildrenback to summary
private void deleteChildren(File file) throws IOException
fetchback to summary
private FetchResult fetch(Repository clonedRepo, URIish u) throws URISyntaxException, TransportException, IOException, GitAPIException
findBranchToCheckoutback to summary
private Ref findBranchToCheckout(FetchResult result)
getDirectoryback to summary
pack-priv File getDirectory()

Get the git directory. This is primarily used for tests.

Returns:File

the git directory

Annotations
@Nullable
initback to summary
private Repository init() throws GitAPIException
isNonEmptyDirectoryback to summary
private static boolean isNonEmptyDirectory(File dir)
parseCommitback to summary
private RevCommit parseCommit(Repository clonedRepo, Ref ref) throws MissingObjectException, IncorrectObjectTypeException, IOException
setBareback to summary
public CloneCommand setBare(boolean bare) throws IllegalStateException

Set whether the cloned repository shall be bare

Parameters
bare:boolean

whether the cloned repository is bare or not

Returns:CloneCommand

this instance

Exceptions
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
setBranchback to summary
public CloneCommand setBranch(String branch)

Set the initial branch

Parameters
branch:String

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 passing null or HEAD.

Returns:CloneCommand

this instance

setBranchesToCloneback to summary
public CloneCommand setBranchesToClone(Collection<String> branchesToClone)

Set the branches or tags to clone.

This is ignored if setCloneAllBranches(true) or setMirror(true) is used. If branchesToClone is null or empty, it's also ignored.

Parameters
branchesToClone:Collection<String>

collection of branches to clone. Must be specified as full ref names (e.g. refs/heads/master or refs/tags/v1.0.0).

Returns:CloneCommand

this

setCallbackback to summary
public CloneCommand setCallback(CloneCommand.Callback callback)

Register a progress callback.

Parameters
callback:CloneCommand.Callback

the callback

Returns:CloneCommand

this

Since
4.8
setCloneAllBranchesback to summary
public CloneCommand setCloneAllBranches(boolean cloneAllBranches)

Set whether all branches have to be fetched.

If false, use setBranchesToClone(Collection) to define what will be cloned. If neither are set, all branches will be cloned.

Parameters
cloneAllBranches:boolean

true when all branches have to be fetched (indicates wildcard in created fetch refspec), false otherwise.

Returns:CloneCommand

this

setCloneSubmodulesback to summary
public CloneCommand setCloneSubmodules(boolean cloneSubmodules)

Set whether to clone submodules

Parameters
cloneSubmodules:boolean

true to initialize and update submodules. Ignored when setBare(boolean) is set to true.

Returns:CloneCommand

this

setDepthback to summary
public CloneCommand setDepth(int depth)

Creates a shallow clone with a history truncated to the specified number of commits.

Parameters
depth:int

the depth

Returns:CloneCommand

this

Since
6.3
setDirectoryback to summary
public CloneCommand setDirectory(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:File

the directory to clone to, or null if the directory name should be taken from the source uri

Returns:CloneCommand

this instance

Exceptions
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()
setFetchTypeback to summary
private void setFetchType()
setFsback to summary
public CloneCommand setFs(FS fs)

Set the file system abstraction to be used for repositories created by this command.

Parameters
fs:FS

the abstraction.

Returns:CloneCommand

this (for chaining calls).

Since
4.10
setGitDirback to summary
public CloneCommand setGitDir(File gitDir)

Set the repository meta directory (.git)

Parameters
gitDir:File

the repository meta directory, or null to choose one automatically at clone time

Returns:CloneCommand

this instance

Exceptions
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
setMirrorback to summary
public CloneCommand setMirror(boolean mirror)

Set up a mirror of the source repository. This implies that a bare repository will be created. Compared to setBare, #setMirror not 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:boolean

whether to mirror all refs from the source repository

Returns:CloneCommand

this

Since
5.6
setNoCheckoutback to summary
public CloneCommand setNoCheckout(boolean noCheckout)

Set whether to skip checking out a branch

Parameters
noCheckout:boolean

if set to true no 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:CloneCommand

this

setNoTagsback to summary
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:CloneCommand

this

Since
5.8
setProgressMonitorback to summary
public CloneCommand setProgressMonitor(ProgressMonitor monitor)

The progress monitor associated with the clone operation. By default, this is set to NullProgressMonitor

Parameters
monitor:ProgressMonitor

a org.eclipse.jgit.lib.ProgressMonitor

Returns:CloneCommand

this

See Also
NullProgressMonitor
setRelativePathsback to summary
public CloneCommand setRelativePaths(boolean relativePaths)

Set whether the cloned repository shall use relative paths for GIT_DIR and GIT_WORK_TREE

Parameters
relativePaths:boolean

if true, use relative paths for GIT_DIR and GIT_WORK_TREE

Returns:CloneCommand

this instance

Since
7.2
setRemoteback to summary
public CloneCommand setRemote(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 of Constants.DEFAULT_REMOTE_NAME will be used.

Parameters
remote:String

name that keeps track of the upstream repository. null means to use DEFAULT_REMOTE_NAME.

Returns:CloneCommand

this instance

See Also
Constants#DEFAULT_REMOTE_NAME
setShallowSinceback to summary
public CloneCommand setShallowSince(OffsetDateTime shallowSince)

Creates a shallow clone with a history after the specified time.

Parameters
shallowSince:OffsetDateTime

the timestammp; must not be null

Returns:CloneCommand

this

Since
6.3
setShallowSinceback to summary
public CloneCommand setShallowSince(Instant shallowSince)

Creates a shallow clone with a history after the specified time.

Parameters
shallowSince:Instant

the timestammp; must not be null

Returns:CloneCommand

this

Since
6.3
setTagOptionback to summary
public CloneCommand setTagOption(TagOpt tagOption)

Set the tag option used for the remote configuration explicitly.

Parameters
tagOption:TagOpt

tag option to be used for the remote config

Returns:CloneCommand

this

Since
5.8
setURIback to summary
public CloneCommand setURI(String uri)

Set the URI to clone from

Parameters
uri:String

the URI to clone from, or null to unset the URI. The URI must be set before call is called.

Returns:CloneCommand

this instance

validateDirsback to summary
private static void validateDirs(File directory, File gitDir, boolean bare) throws IllegalStateException
verifyDirectoriesback to summary
pack-priv void verifyDirectories(URIish u)
org.eclipse.jgit.api back to summary

public Interface CloneCommand.Callback


Callback for status of clone operation.
Since
4.8

Method Summary

Modifier and TypeMethod and Description
public void
checkingOut(AnyObjectId
the id of the commit being checked out
commit
,
String
the submodule path
path
)

Notify checkout of commit

public void
cloningSubmodule(String
the submodule path
path
)

Notify starting to clone a submodule.

public void
initializedSubmodules(Collection<String>
the submodules
submodules
)

Notify initialized submodules.

Method Detail

checkingOutback to summary
public void checkingOut(AnyObjectId commit, String path)

Notify checkout of commit

Parameters
commit:AnyObjectId

the id of the commit being checked out

path:String

the submodule path

cloningSubmoduleback to summary
public void cloningSubmodule(String path)

Notify starting to clone a submodule.

Parameters
path:String

the submodule path

initializedSubmodulesback to summary
public void initializedSubmodules(Collection<String> submodules)

Notify initialized submodules.

Parameters
submodules:Collection<String>

the submodules

org.eclipse.jgit.api back to summary

private final Enum CloneCommand.FETCH_TYPE

extends Enum<CloneCommand.FETCH_TYPE>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final CloneCommand.FETCH_TYPE
public static final CloneCommand.FETCH_TYPE
public static final CloneCommand.FETCH_TYPE

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static CloneCommand.FETCH_TYPE
public static CloneCommand.FETCH_TYPE[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

ALL_BRANCHESback to summary
public static final CloneCommand.FETCH_TYPE ALL_BRANCHES
MIRRORback to summary
public static final CloneCommand.FETCH_TYPE MIRROR
MULTIPLE_BRANCHESback to summary
public static final CloneCommand.FETCH_TYPE MULTIPLE_BRANCHES

Constructor Detail

FETCH_TYPEback to summary
private FETCH_TYPE()

Method Detail

valueOfback to summary
public static CloneCommand.FETCH_TYPE valueOf(String name)
valuesback to summary
public static CloneCommand.FETCH_TYPE[] values()