From 9d7697e44ea5ef2c49bbe0566c8e85fec4e8e12f Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Wed, 28 Sep 2022 09:19:26 +0200
Subject: [PATCH] fix(git): clone submodules after repo config (#18014)

---
 lib/util/git/index.ts                | 38 ++++++++++++++++++----------
 lib/workers/repository/init/index.ts |  3 ++-
 2 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/lib/util/git/index.ts b/lib/util/git/index.ts
index 55b466ae0b..d357441b60 100644
--- a/lib/util/git/index.ts
+++ b/lib/util/git/index.ts
@@ -156,6 +156,7 @@ let config: LocalConfig = {} as any;
 // TODO: can be undefined
 let git: SimpleGit;
 let gitInitialized: boolean;
+let submodulesInitizialized: boolean;
 
 let privateKeySet = false;
 
@@ -236,6 +237,7 @@ export async function initRepo(args: StorageConfig): Promise<void> {
     LC_ALL: 'C',
   });
   gitInitialized = false;
+  submodulesInitizialized = false;
   await fetchBranchCommits();
 }
 
@@ -341,6 +343,26 @@ export async function getSubmodules(): Promise<string[]> {
   }
 }
 
+export async function cloneSubmodules(shouldClone: boolean): Promise<void> {
+  if (!shouldClone || submodulesInitizialized) {
+    return;
+  }
+  submodulesInitizialized = true;
+  await syncGit();
+  const submodules = await getSubmodules();
+  for (const submodule of submodules) {
+    try {
+      logger.debug(`Cloning git submodule at ${submodule}`);
+      await gitRetry(() => git.submoduleUpdate(['--init', submodule]));
+    } catch (err) {
+      logger.warn(
+        { err },
+        `Unable to initialise git submodule at ${submodule}`
+      );
+    }
+  }
+}
+
 export async function syncGit(): Promise<void> {
   if (gitInitialized) {
     return;
@@ -415,20 +437,8 @@ export async function syncGit(): Promise<void> {
     }
     throw err;
   }
-  if (config.cloneSubmodules) {
-    const submodules = await getSubmodules();
-    for (const submodule of submodules) {
-      try {
-        logger.debug(`Cloning git submodule at ${submodule}`);
-        await gitRetry(() => git.submoduleUpdate(['--init', submodule]));
-      } catch (err) {
-        logger.warn(
-          { err },
-          `Unable to initialise git submodule at ${submodule}`
-        );
-      }
-    }
-  }
+  // This will only happen now if set in global config
+  await cloneSubmodules(!!config.cloneSubmodules);
   try {
     const latestCommit = (await git.log({ n: 1 })).latest;
     logger.debug({ latestCommit }, 'latest repository commit');
diff --git a/lib/workers/repository/init/index.ts b/lib/workers/repository/init/index.ts
index 199e3330f9..c07d0ef73b 100644
--- a/lib/workers/repository/init/index.ts
+++ b/lib/workers/repository/init/index.ts
@@ -4,7 +4,7 @@ import type { RenovateConfig } from '../../../config/types';
 import { logger } from '../../../logger';
 import { platform } from '../../../modules/platform';
 import { clone } from '../../../util/clone';
-import { setUserRepoConfig } from '../../../util/git';
+import { cloneSubmodules, setUserRepoConfig } from '../../../util/git';
 import { getAll } from '../../../util/host-rules';
 import { checkIfConfigured } from '../configured';
 import { PackageFiles } from '../package-files';
@@ -48,5 +48,6 @@ export async function initRepo(
       'Full resolved config and hostRules including presets'
     );
   }
+  await cloneSubmodules(!!config.cloneSubmodules);
   return config;
 }
-- 
GitLab