Skip to content

Commit d0c8281

Browse files
committed
Git shallow clone for better performance
Do not download the entire commit history from the remote. Execute the equivalent of `git clone --depth 1` Fixes spring-cloud#1544
1 parent c372fcf commit d0c8281

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

docs/modules/ROOT/pages/server/environment-repository/git-backend.adoc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,20 @@ All other repositories are not cloned until configuration from the repository is
189189
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.
190190
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.
191191

192+
By default, the server clones the entire commit history from remote repositories.
193+
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:
194+
195+
[source,yaml]
196+
----
197+
spring:
198+
cloud:
199+
config:
200+
server:
201+
git:
202+
uri: https://github.com/spring-cloud-samples/config-repo
203+
clone-shallow: true
204+
----
205+
192206
[[authentication]]
193207
== Authentication
194208

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentProperties.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
/**
2929
* @author Dylan Roberts
3030
* @author Gareth Clay
31+
* @author Chin Huang
3132
*/
3233
public class JGitEnvironmentProperties extends AbstractScmAccessorProperties
3334
implements HttpEnvironmentRepositoryProperties {
@@ -56,6 +57,13 @@ public class JGitEnvironmentProperties extends AbstractScmAccessorProperties
5657
*/
5758
private boolean cloneOnStart = false;
5859

60+
/**
61+
* Flag to indicate that the commit history in the clone should be truncated to one
62+
* commit. Generally leads to faster queries because the entire commit history is not
63+
* downloaded from the remote.
64+
*/
65+
private boolean cloneShallow = false;
66+
5967
/**
6068
* Flag to indicate that the submodules in the repository should be cloned.
6169
*/
@@ -148,6 +156,14 @@ public void setCloneOnStart(boolean cloneOnStart) {
148156
this.cloneOnStart = cloneOnStart;
149157
}
150158

159+
public boolean isCloneShallow() {
160+
return this.cloneShallow;
161+
}
162+
163+
public void setCloneShallow(boolean cloneShallow) {
164+
this.cloneShallow = cloneShallow;
165+
}
166+
151167
public boolean isCloneSubmodules() {
152168
return this.cloneSubmodules;
153169
}

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
* @author Ryan Lynch
7878
* @author Gareth Clay
7979
* @author ChaoDong Xi
80+
* @author Chin Huang
8081
*/
8182
public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
8283
implements EnvironmentRepository, SearchPathLocator, InitializingBean {
@@ -159,7 +160,7 @@ public JGitEnvironmentRepository(ConfigurableEnvironment environment, JGitEnviro
159160
this.deleteUntrackedBranches = properties.isDeleteUntrackedBranches();
160161
this.refreshRate = properties.getRefreshRate();
161162
this.skipSslValidation = properties.isSkipSslValidation();
162-
this.gitFactory = new JGitFactory(properties.isCloneSubmodules());
163+
this.gitFactory = new JGitFactory(properties.isCloneShallow(), properties.isCloneSubmodules());
163164
this.tryMasterBranch = properties.isTryMasterBranch();
164165
this.observationRegistry = observationRegistry;
165166
}
@@ -756,13 +757,16 @@ public void setLastRefresh(long lastRefresh) {
756757
*/
757758
public static class JGitFactory {
758759

760+
private final boolean cloneShallow;
761+
759762
private final boolean cloneSubmodules;
760763

761764
public JGitFactory() {
762-
this(false);
765+
this(false, false);
763766
}
764767

765-
public JGitFactory(boolean cloneSubmodules) {
768+
public JGitFactory(boolean cloneShallow, boolean cloneSubmodules) {
769+
this.cloneShallow = cloneShallow;
766770
this.cloneSubmodules = cloneSubmodules;
767771
}
768772

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

774778
public CloneCommand getCloneCommandByCloneRepository() {
775779
CloneCommand command = Git.cloneRepository().setCloneSubmodules(cloneSubmodules);
780+
if (cloneShallow) {
781+
command.setDepth(1);
782+
}
776783
return command;
777784
}
778785

0 commit comments

Comments
 (0)