Skip to content

Git shallow clone for better performance #2403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ All other repositories are not cloned until configuration from the repository is
NOTE: Setting a repository to be cloned when the Config Server starts up can help to identify a misconfigured configuration source (such as an invalid repository URI) quickly, while the Config Server is starting up.
With `cloneOnStart` not enabled for a configuration source, the Config Server may start successfully with a misconfigured or invalid configuration source and not detect an error until an application requests configuration from that configuration source.

By default, the server clones the entire commit history from remote repositories.
Downloading a huge commit history might be slow, so the server can be configured to truncate the commit history in the clone to one commit, as shown in the following top-level example:

[source,yaml]
----
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
clone-shallow: true
----

[[authentication]]
== Authentication

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
/**
* @author Dylan Roberts
* @author Gareth Clay
* @author Chin Huang
*/
public class JGitEnvironmentProperties extends AbstractScmAccessorProperties
implements HttpEnvironmentRepositoryProperties {
Expand Down Expand Up @@ -56,6 +57,13 @@ public class JGitEnvironmentProperties extends AbstractScmAccessorProperties
*/
private boolean cloneOnStart = false;

/**
* Flag to indicate that the commit history in the clone should be truncated to one
* commit. Generally leads to faster queries because the entire commit history is not
* downloaded from the remote.
*/
private boolean cloneShallow = false;

/**
* Flag to indicate that the submodules in the repository should be cloned.
*/
Expand Down Expand Up @@ -148,6 +156,14 @@ public void setCloneOnStart(boolean cloneOnStart) {
this.cloneOnStart = cloneOnStart;
}

public boolean isCloneShallow() {
return this.cloneShallow;
}

public void setCloneShallow(boolean cloneShallow) {
this.cloneShallow = cloneShallow;
}

public boolean isCloneSubmodules() {
return this.cloneSubmodules;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
* @author Ryan Lynch
* @author Gareth Clay
* @author ChaoDong Xi
* @author Chin Huang
*/
public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
implements EnvironmentRepository, SearchPathLocator, InitializingBean {
Expand Down Expand Up @@ -159,7 +160,7 @@ public JGitEnvironmentRepository(ConfigurableEnvironment environment, JGitEnviro
this.deleteUntrackedBranches = properties.isDeleteUntrackedBranches();
this.refreshRate = properties.getRefreshRate();
this.skipSslValidation = properties.isSkipSslValidation();
this.gitFactory = new JGitFactory(properties.isCloneSubmodules());
this.gitFactory = new JGitFactory(properties.isCloneShallow(), properties.isCloneSubmodules());
this.tryMasterBranch = properties.isTryMasterBranch();
this.observationRegistry = observationRegistry;
}
Expand Down Expand Up @@ -756,13 +757,16 @@ public void setLastRefresh(long lastRefresh) {
*/
public static class JGitFactory {

private final boolean cloneShallow;

private final boolean cloneSubmodules;

public JGitFactory() {
this(false);
this(false, false);
}

public JGitFactory(boolean cloneSubmodules) {
public JGitFactory(boolean cloneShallow, boolean cloneSubmodules) {
this.cloneShallow = cloneShallow;
this.cloneSubmodules = cloneSubmodules;
}

Expand All @@ -773,6 +777,9 @@ public Git getGitByOpen(File file) throws IOException {

public CloneCommand getCloneCommandByCloneRepository() {
CloneCommand command = Git.cloneRepository().setCloneSubmodules(cloneSubmodules);
if (cloneShallow) {
command.setDepth(1);
}
return command;
}

Expand Down