Skip to content

Commit a5786c0

Browse files
committed
Git shallow clone for better performance
By default, the server clones the entire commit history from a remote repository. Downloading a huge commit history might be slow, so add a configuration property to truncate the commit history to a few commits. Fixes #1544
1 parent a35ae66 commit a5786c0

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
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 a remote repository.
193+
Downloading a huge commit history might be slow, so the server can be configured to truncate the commit history to a few commits, 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+
depth: 1
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 {
@@ -61,6 +62,13 @@ public class JGitEnvironmentProperties extends AbstractScmAccessorProperties
6162
*/
6263
private boolean cloneSubmodules = false;
6364

65+
/**
66+
* If greater than zero, then truncate the history to the specified number of commits.
67+
* Generally leads to faster queries because the entire history is not downloaded from
68+
* the remote.
69+
*/
70+
private int depth = 0;
71+
6472
/**
6573
* Flag to indicate that the repository should force pull. If true discard any local
6674
* changes and take from remote repository.
@@ -156,6 +164,14 @@ public void setCloneSubmodules(boolean cloneSubmodules) {
156164
this.cloneSubmodules = cloneSubmodules;
157165
}
158166

167+
public int getDepth() {
168+
return this.depth;
169+
}
170+
171+
public void setDepth(int depth) {
172+
this.depth = depth;
173+
}
174+
159175
public boolean isForcePull() {
160176
return this.forcePull;
161177
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
* @author Ryan Lynch
7979
* @author Gareth Clay
8080
* @author ChaoDong Xi
81+
* @author Chin Huang
8182
*/
8283
public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
8384
implements EnvironmentRepository, SearchPathLocator, InitializingBean {
@@ -167,7 +168,7 @@ public JGitEnvironmentRepository(ConfigurableEnvironment environment, JGitEnviro
167168
this.deleteUntrackedBranches = properties.isDeleteUntrackedBranches();
168169
this.refreshRate = properties.getRefreshRate();
169170
this.skipSslValidation = properties.isSkipSslValidation();
170-
this.gitFactory = new JGitFactory(properties.isCloneSubmodules());
171+
this.gitFactory = new JGitFactory(properties.isCloneSubmodules(), properties.getDepth());
171172
this.tryMasterBranch = properties.isTryMasterBranch();
172173
this.observationRegistry = observationRegistry;
173174
}
@@ -778,12 +779,19 @@ public static class JGitFactory {
778779

779780
private final boolean cloneSubmodules;
780781

782+
private final int depth;
783+
781784
public JGitFactory() {
782-
this(false);
785+
this(false, 0);
783786
}
784787

785788
public JGitFactory(boolean cloneSubmodules) {
789+
this(cloneSubmodules, 0);
790+
}
791+
792+
public JGitFactory(boolean cloneSubmodules, int depth) {
786793
this.cloneSubmodules = cloneSubmodules;
794+
this.depth = depth;
787795
}
788796

789797
public Git getGitByOpen(File file) throws IOException {
@@ -793,6 +801,9 @@ public Git getGitByOpen(File file) throws IOException {
793801

794802
public CloneCommand getCloneCommandByCloneRepository() {
795803
CloneCommand command = Git.cloneRepository().setCloneSubmodules(cloneSubmodules);
804+
if (depth > 0) {
805+
command.setDepth(depth);
806+
}
796807
return command;
797808
}
798809

0 commit comments

Comments
 (0)