Skip to content

Commit

Permalink
Git shallow clone for better performance
Browse files Browse the repository at this point in the history
Do not download the entire commit history from the remote.  Execute the
equivalent of `git clone --depth 1`

Fixes spring-cloud#1544
  • Loading branch information
pukkaone committed Apr 9, 2024
1 parent c372fcf commit d0c8281
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
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

0 comments on commit d0c8281

Please sign in to comment.