From ac7af06d808998f921ab0d2667ea9471e9577200 Mon Sep 17 00:00:00 2001
From: Xavier Chapron <xavier.chapron@stimio.fr>
Date: Fri, 29 Oct 2021 10:54:32 +0200
Subject: [PATCH 1/5] Add reset and clean for submodules

---
 dist/index.js              | 22 ++++++++++++++++++++++
 src/git-command-manager.ts | 22 ++++++++++++++++++++++
 src/git-source-provider.ts |  2 ++
 3 files changed, 46 insertions(+)

diff --git a/dist/index.js b/dist/index.js
index 7155f1c..9ec08b0 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -7101,6 +7101,26 @@ class GitCommandManager {
             yield this.execGit(args);
         });
     }
+    submoduleReset(recursive) {
+        return __awaiter(this, void 0, void 0, function* () {
+            const args = ['submodule', 'foreach'];
+            if (recursive) {
+                args.push('--recursive');
+            }
+            args.push('git reset --hard');
+            yield this.execGit(args);
+        });
+    }
+    submoduleClean(recursive) {
+        return __awaiter(this, void 0, void 0, function* () {
+            const args = ['submodule', 'foreach'];
+            if (recursive) {
+                args.push('--recursive');
+            }
+            args.push('git clean -ffdx');
+            yield this.execGit(args);
+        });
+    }
     tagExists(pattern) {
         return __awaiter(this, void 0, void 0, function* () {
             const output = yield this.execGit(['tag', '--list', pattern]);
@@ -7416,6 +7436,8 @@ function getSource(settings) {
                     core.startGroup('Fetching submodules');
                     yield git.submoduleSync(settings.nestedSubmodules);
                     yield git.submoduleUpdate(settings.fetchDepth, settings.nestedSubmodules);
+                    yield git.submoduleReset(settings.nestedSubmodules);
+                    yield git.submoduleClean(settings.nestedSubmodules);
                     yield git.submoduleForeach('git config --local gc.auto 0', settings.nestedSubmodules);
                     core.endGroup();
                     // Persist credentials
diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts
index 409a161..6932e96 100644
--- a/src/git-command-manager.ts
+++ b/src/git-command-manager.ts
@@ -40,6 +40,8 @@ export interface IGitCommandManager {
   submoduleForeach(command: string, recursive: boolean): Promise<string>
   submoduleSync(recursive: boolean): Promise<void>
   submoduleUpdate(fetchDepth: number, recursive: boolean): Promise<void>
+  submoduleReset(recursive: boolean): Promise<void>
+  submoduleClean(recursive: boolean): Promise<void>
   tagExists(pattern: string): Promise<boolean>
   tryClean(): Promise<boolean>
   tryConfigUnset(configKey: string, globalConfig?: boolean): Promise<boolean>
@@ -324,6 +326,26 @@ class GitCommandManager {
     await this.execGit(args)
   }
 
+  async submoduleReset(recursive: boolean): Promise<void> {
+    const args = ['submodule', 'foreach']
+    if (recursive) {
+      args.push('--recursive')
+    }
+    args.push('git reset --hard')
+
+    await this.execGit(args)
+  }
+
+  async submoduleClean(recursive: boolean): Promise<void> {
+    const args = ['submodule', 'foreach']
+    if (recursive) {
+      args.push('--recursive')
+    }
+    args.push('git clean -ffdx')
+
+    await this.execGit(args)
+  }
+
   async tagExists(pattern: string): Promise<boolean> {
     const output = await this.execGit(['tag', '--list', pattern])
     return !!output.stdout.trim()
diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts
index 42a12e0..b95ce3a 100644
--- a/src/git-source-provider.ts
+++ b/src/git-source-provider.ts
@@ -183,6 +183,8 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
           settings.fetchDepth,
           settings.nestedSubmodules
         )
+        await git.submoduleReset(settings.nestedSubmodules)
+        await git.submoduleClean(settings.nestedSubmodules)
         await git.submoduleForeach(
           'git config --local gc.auto 0',
           settings.nestedSubmodules

From 30cd0465ca954484b26b37e13ce00af381bfa678 Mon Sep 17 00:00:00 2001
From: Laura Bailey <laurab@adaptsolutions.com.au>
Date: Thu, 24 Feb 2022 13:41:42 +1100
Subject: [PATCH 2/5] Move reset and clean before update

---
 src/git-source-provider.ts | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts
index b95ce3a..487385c 100644
--- a/src/git-source-provider.ts
+++ b/src/git-source-provider.ts
@@ -176,6 +176,10 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
         await authHelper.configureGlobalAuth()
         core.endGroup()
 
+        // Clean existing submodules
+        await git.submoduleReset(settings.nestedSubmodules)
+        await git.submoduleClean(settings.nestedSubmodules)
+
         // Checkout submodules
         core.startGroup('Fetching submodules')
         await git.submoduleSync(settings.nestedSubmodules)
@@ -183,8 +187,6 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
           settings.fetchDepth,
           settings.nestedSubmodules
         )
-        await git.submoduleReset(settings.nestedSubmodules)
-        await git.submoduleClean(settings.nestedSubmodules)
         await git.submoduleForeach(
           'git config --local gc.auto 0',
           settings.nestedSubmodules

From dde2556f7ae6d80cb2f3a361383d89b8f5bd45ef Mon Sep 17 00:00:00 2001
From: Laura Bailey <laurab@adaptsolutions.com.au>
Date: Thu, 24 Feb 2022 13:42:30 +1100
Subject: [PATCH 3/5] Only clean submodules when clean is set to true

---
 src/git-source-provider.ts | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts
index 487385c..a96afba 100644
--- a/src/git-source-provider.ts
+++ b/src/git-source-provider.ts
@@ -177,8 +177,11 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
         core.endGroup()
 
         // Clean existing submodules
-        await git.submoduleReset(settings.nestedSubmodules)
-        await git.submoduleClean(settings.nestedSubmodules)
+        if (settings.clean)
+        {
+            await git.submoduleReset(settings.nestedSubmodules)
+            await git.submoduleClean(settings.nestedSubmodules)
+        }
 
         // Checkout submodules
         core.startGroup('Fetching submodules')

From 390521655e25279683d21a7b6614766c1f6fd78a Mon Sep 17 00:00:00 2001
From: Laura Bailey <laurab@adaptsolutions.com.au>
Date: Tue, 22 Feb 2022 16:55:33 +1100
Subject: [PATCH 4/5] Fix unimplemented methods

---
 __test__/git-auth-helper.test.ts      | 2 ++
 __test__/git-directory-helper.test.ts | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/__test__/git-auth-helper.test.ts b/__test__/git-auth-helper.test.ts
index b39d352..10f6636 100644
--- a/__test__/git-auth-helper.test.ts
+++ b/__test__/git-auth-helper.test.ts
@@ -733,6 +733,8 @@ async function setup(testName: string): Promise<void> {
     }),
     submoduleSync: jest.fn(),
     submoduleUpdate: jest.fn(),
+    submoduleReset: jest.fn(),
+    submoduleClean: jest.fn(),
     tagExists: jest.fn(),
     tryClean: jest.fn(),
     tryConfigUnset: jest.fn(
diff --git a/__test__/git-directory-helper.test.ts b/__test__/git-directory-helper.test.ts
index 70849b5..e6d3cd0 100644
--- a/__test__/git-directory-helper.test.ts
+++ b/__test__/git-directory-helper.test.ts
@@ -423,6 +423,8 @@ async function setup(testName: string): Promise<void> {
     submoduleForeach: jest.fn(),
     submoduleSync: jest.fn(),
     submoduleUpdate: jest.fn(),
+    submoduleReset: jest.fn(),
+    submoduleClean: jest.fn(),
     tagExists: jest.fn(),
     tryClean: jest.fn(async () => {
       return true

From 37332a5498d4939d8481006e4d187a6d31c72411 Mon Sep 17 00:00:00 2001
From: Laura Bailey <laurab@adaptsolutions.com.au>
Date: Fri, 25 Feb 2022 16:55:52 +1100
Subject: [PATCH 5/5] Compiled index.js

I had to add `"newLine": "LF"` to the tsconfig.json. I also had to discard a change to `module.exports` on line 4232 because I'm pretty sure it had nothing to do with my changes.
---
 dist/index.js | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/dist/index.js b/dist/index.js
index 9ec08b0..cd07c92 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -7432,12 +7432,15 @@ function getSource(settings) {
                     core.startGroup('Setting up auth for fetching submodules');
                     yield authHelper.configureGlobalAuth();
                     core.endGroup();
+                    // Clean existing submodules
+                    if (settings.clean) {
+                        yield git.submoduleReset(settings.nestedSubmodules);
+                        yield git.submoduleClean(settings.nestedSubmodules);
+                    }
                     // Checkout submodules
                     core.startGroup('Fetching submodules');
                     yield git.submoduleSync(settings.nestedSubmodules);
                     yield git.submoduleUpdate(settings.fetchDepth, settings.nestedSubmodules);
-                    yield git.submoduleReset(settings.nestedSubmodules);
-                    yield git.submoduleClean(settings.nestedSubmodules);
                     yield git.submoduleForeach('git config --local gc.auto 0', settings.nestedSubmodules);
                     core.endGroup();
                     // Persist credentials