Skip to content

Commit 0e2d78f

Browse files
committed
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 <[email protected]>
1 parent a034251 commit 0e2d78f

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.eclipse.jgit.api.ResetCommand.ResetType;
4141
import org.eclipse.jgit.api.Status;
4242
import org.eclipse.jgit.api.StatusCommand;
43+
import org.eclipse.jgit.api.SubmoduleUpdateCommand;
4344
import org.eclipse.jgit.api.TransportCommand;
4445
import org.eclipse.jgit.api.TransportConfigCallback;
4546
import org.eclipse.jgit.api.errors.GitAPIException;
@@ -113,7 +114,7 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
113114
*/
114115
private boolean cloneOnStart;
115116

116-
private JGitEnvironmentRepository.JGitFactory gitFactory;
117+
private JGitEnvironmentRepository.JGitFactory gitFactory = new JGitFactory();
117118

118119
private String defaultLabel;
119120

@@ -146,6 +147,8 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository
146147
*/
147148
private boolean skipSslValidation;
148149

150+
private boolean cloneSubmodules;
151+
149152
private boolean tryMasterBranch;
150153

151154
private final ObservationRegistry observationRegistry;
@@ -167,11 +170,19 @@ public JGitEnvironmentRepository(ConfigurableEnvironment environment, JGitEnviro
167170
this.deleteUntrackedBranches = properties.isDeleteUntrackedBranches();
168171
this.refreshRate = properties.getRefreshRate();
169172
this.skipSslValidation = properties.isSkipSslValidation();
170-
this.gitFactory = new JGitFactory(properties.isCloneSubmodules());
173+
this.cloneSubmodules = properties.isCloneSubmodules();
171174
this.tryMasterBranch = properties.isTryMasterBranch();
172175
this.observationRegistry = observationRegistry;
173176
}
174177

178+
public boolean isCloneSubmodules() {
179+
return this.cloneSubmodules;
180+
}
181+
182+
public void setCloneSubmodules(boolean cloneSubmodules) {
183+
this.cloneSubmodules = cloneSubmodules;
184+
}
185+
175186
public boolean isTryMasterBranch() {
176187
return tryMasterBranch;
177188
}
@@ -315,6 +326,13 @@ public String refresh(String label) {
315326
if (this.deleteUntrackedBranches && fetchStatus != null) {
316327
deleteUntrackedLocalBranches(fetchStatus.getTrackingRefUpdates(), git);
317328
}
329+
330+
// update submodules if needed
331+
if (cloneSubmodules) {
332+
SubmoduleUpdateCommand submoduleUpdate = git.submoduleUpdate().setFetch(true);
333+
configureCommand(submoduleUpdate);
334+
submoduleUpdate.call();
335+
}
318336
}
319337

320338
// checkout after fetch so we can get any new branches, tags, ect.
@@ -669,6 +687,7 @@ private Git copyFromLocalRepository() throws IOException {
669687

670688
private Git cloneToBasedir() throws GitAPIException {
671689
CloneCommand clone = this.gitFactory.getCloneCommandByCloneRepository()
690+
.setCloneSubmodules(cloneSubmodules)
672691
.setURI(getUri())
673692
.setDirectory(getBasedir());
674693
configureCommand(clone);
@@ -776,23 +795,13 @@ public void setLastRefresh(long lastRefresh) {
776795
*/
777796
public static class JGitFactory {
778797

779-
private final boolean cloneSubmodules;
780-
781-
public JGitFactory() {
782-
this(false);
783-
}
784-
785-
public JGitFactory(boolean cloneSubmodules) {
786-
this.cloneSubmodules = cloneSubmodules;
787-
}
788-
789798
public Git getGitByOpen(File file) throws IOException {
790799
Git git = Git.open(file);
791800
return git;
792801
}
793802

794803
public CloneCommand getCloneCommandByCloneRepository() {
795-
CloneCommand command = Git.cloneRepository().setCloneSubmodules(cloneSubmodules);
804+
CloneCommand command = Git.cloneRepository();
796805
return command;
797806
}
798807

0 commit comments

Comments
 (0)