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 a remote repository.
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:

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

[[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 @@ -61,6 +62,13 @@ public class JGitEnvironmentProperties extends AbstractScmAccessorProperties
*/
private boolean cloneSubmodules = false;

/**
* If greater than zero, then truncate the history to the specified number of commits.
* Generally leads to faster queries because the entire history is not downloaded from
* the remote.
*/
private int depth = 0;

/**
* Flag to indicate that the repository should force pull. If true discard any local
* changes and take from remote repository.
Expand Down Expand Up @@ -156,6 +164,14 @@ public void setCloneSubmodules(boolean cloneSubmodules) {
this.cloneSubmodules = cloneSubmodules;
}

public int getDepth() {
return this.depth;
}

public void setDepth(int depth) {
this.depth = depth;
}

public boolean isForcePull() {
return this.forcePull;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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 @@ -167,7 +168,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.isCloneSubmodules(), properties.getDepth());
this.tryMasterBranch = properties.isTryMasterBranch();
this.observationRegistry = observationRegistry;
}
Expand Down Expand Up @@ -778,12 +779,19 @@ public static class JGitFactory {

private final boolean cloneSubmodules;

private final int depth;

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

public JGitFactory(boolean cloneSubmodules) {
this(cloneSubmodules, 0);
}

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

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

public CloneCommand getCloneCommandByCloneRepository() {
CloneCommand command = Git.cloneRepository().setCloneSubmodules(cloneSubmodules);
if (depth > 0) {
command.setDepth(depth);
}
return command;
}

Expand Down