From 0e2d78f50586b38bd9ac94b3feac017e8d9502e9 Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Wed, 21 May 2025 19:51:34 +0200 Subject: [PATCH] Add support for updating submodules on cloud config server repo refresh - if cloneSubmodules is enabled, when repo is being refreshed, also properly fetch submodule updates - move cloneSubmodules boolean outside of JGitFactory because this property can be changed during runtime and there is no reason for this boolean to be copied inside Closes #1924 Signed-off-by: Tomas Slusny --- .../JGitEnvironmentRepository.java | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java index 66c34b9f50..5287ae8635 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java @@ -40,6 +40,7 @@ import org.eclipse.jgit.api.ResetCommand.ResetType; import org.eclipse.jgit.api.Status; import org.eclipse.jgit.api.StatusCommand; +import org.eclipse.jgit.api.SubmoduleUpdateCommand; import org.eclipse.jgit.api.TransportCommand; import org.eclipse.jgit.api.TransportConfigCallback; import org.eclipse.jgit.api.errors.GitAPIException; @@ -113,7 +114,7 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository */ private boolean cloneOnStart; - private JGitEnvironmentRepository.JGitFactory gitFactory; + private JGitEnvironmentRepository.JGitFactory gitFactory = new JGitFactory(); private String defaultLabel; @@ -146,6 +147,8 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository */ private boolean skipSslValidation; + private boolean cloneSubmodules; + private boolean tryMasterBranch; private final ObservationRegistry observationRegistry; @@ -167,11 +170,19 @@ public JGitEnvironmentRepository(ConfigurableEnvironment environment, JGitEnviro this.deleteUntrackedBranches = properties.isDeleteUntrackedBranches(); this.refreshRate = properties.getRefreshRate(); this.skipSslValidation = properties.isSkipSslValidation(); - this.gitFactory = new JGitFactory(properties.isCloneSubmodules()); + this.cloneSubmodules = properties.isCloneSubmodules(); this.tryMasterBranch = properties.isTryMasterBranch(); this.observationRegistry = observationRegistry; } + public boolean isCloneSubmodules() { + return this.cloneSubmodules; + } + + public void setCloneSubmodules(boolean cloneSubmodules) { + this.cloneSubmodules = cloneSubmodules; + } + public boolean isTryMasterBranch() { return tryMasterBranch; } @@ -315,6 +326,13 @@ public String refresh(String label) { if (this.deleteUntrackedBranches && fetchStatus != null) { deleteUntrackedLocalBranches(fetchStatus.getTrackingRefUpdates(), git); } + + // update submodules if needed + if (cloneSubmodules) { + SubmoduleUpdateCommand submoduleUpdate = git.submoduleUpdate().setFetch(true); + configureCommand(submoduleUpdate); + submoduleUpdate.call(); + } } // checkout after fetch so we can get any new branches, tags, ect. @@ -669,6 +687,7 @@ private Git copyFromLocalRepository() throws IOException { private Git cloneToBasedir() throws GitAPIException { CloneCommand clone = this.gitFactory.getCloneCommandByCloneRepository() + .setCloneSubmodules(cloneSubmodules) .setURI(getUri()) .setDirectory(getBasedir()); configureCommand(clone); @@ -776,23 +795,13 @@ public void setLastRefresh(long lastRefresh) { */ public static class JGitFactory { - private final boolean cloneSubmodules; - - public JGitFactory() { - this(false); - } - - public JGitFactory(boolean cloneSubmodules) { - this.cloneSubmodules = cloneSubmodules; - } - public Git getGitByOpen(File file) throws IOException { Git git = Git.open(file); return git; } public CloneCommand getCloneCommandByCloneRepository() { - CloneCommand command = Git.cloneRepository().setCloneSubmodules(cloneSubmodules); + CloneCommand command = Git.cloneRepository(); return command; }