From 9c15e92f3d3ca76470b39e664689a17fd69ce424 Mon Sep 17 00:00:00 2001
From: Marcus Tillmanns <marcus.tillmanns@qt.io>
Date: Wed, 28 Aug 2024 13:15:20 +0200
Subject: [PATCH] Simplified the submoduleDirectories

---
 __test__/git-auth-helper.test.ts |  2 +-
 __test__/input-helper.test.ts    |  2 +-
 dist/index.js                    | 34 ++++++------------------
 src/git-command-manager.ts       | 44 ++++++++++++--------------------
 src/git-source-settings.ts       |  2 +-
 src/input-helper.ts              |  4 +--
 6 files changed, 29 insertions(+), 59 deletions(-)

diff --git a/__test__/git-auth-helper.test.ts b/__test__/git-auth-helper.test.ts
index ff60baef6..6666dca85 100644
--- a/__test__/git-auth-helper.test.ts
+++ b/__test__/git-auth-helper.test.ts
@@ -813,7 +813,7 @@ async function setup(testName: string): Promise<void> {
     lfs: false,
     submodules: false,
     nestedSubmodules: false,
-    submoduleDirectories: null,
+    submoduleDirectories: [],
     persistCredentials: true,
     ref: 'refs/heads/main',
     repositoryName: 'my-repo',
diff --git a/__test__/input-helper.test.ts b/__test__/input-helper.test.ts
index 35c1848dd..75eb546a6 100644
--- a/__test__/input-helper.test.ts
+++ b/__test__/input-helper.test.ts
@@ -94,7 +94,7 @@ describe('input-helper tests', () => {
     expect(settings.showProgress).toBe(true)
     expect(settings.lfs).toBe(false)
     expect(settings.ref).toBe('refs/heads/some-ref')
-    expect(settings.submoduleDirectories).toBe(null)
+    expect(settings.submoduleDirectories).toStrictEqual([])
     expect(settings.repositoryName).toBe('some-repo')
     expect(settings.repositoryOwner).toBe('some-owner')
     expect(settings.repositoryPath).toBe(gitHubWorkspace)
diff --git a/dist/index.js b/dist/index.js
index aba7b8409..abd0a7554 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -797,30 +797,15 @@ class GitCommandManager {
     }
     submoduleUpdate(fetchDepth, recursive, submoduleDirectories) {
         return __awaiter(this, void 0, void 0, function* () {
-            if (submoduleDirectories) {
-                for (const submodule of submoduleDirectories) {
-                    const args = ['-c', 'protocol.version=2'];
-                    args.push('submodule', 'update', '--init', '--force', submodule);
-                    if (fetchDepth > 0) {
-                        args.push(`--depth=${fetchDepth}`);
-                    }
-                    if (recursive) {
-                        args.push('--recursive');
-                    }
-                    yield this.execGit(args);
-                }
+            const args = ['-c', 'protocol.version=2'];
+            args.push('submodule', 'update', '--init', '--force', ...submoduleDirectories);
+            if (fetchDepth > 0) {
+                args.push(`--depth=${fetchDepth}`);
             }
-            else {
-                const args = ['-c', 'protocol.version=2'];
-                args.push('submodule', 'update', '--init', '--force');
-                if (fetchDepth > 0) {
-                    args.push(`--depth=${fetchDepth}`);
-                }
-                if (recursive) {
-                    args.push('--recursive');
-                }
-                yield this.execGit(args);
+            if (recursive) {
+                args.push('--recursive');
             }
+            yield this.execGit(args);
         });
     }
     submoduleStatus() {
@@ -1820,7 +1805,7 @@ function getInputs() {
         // Submodules
         result.submodules = false;
         result.nestedSubmodules = false;
-        result.submoduleDirectories = null;
+        result.submoduleDirectories = [];
         const submodulesString = (core.getInput('submodules') || '').toUpperCase();
         if (submodulesString == 'RECURSIVE') {
             result.submodules = true;
@@ -1835,9 +1820,6 @@ function getInputs() {
             if (!result.submodules)
                 result.submodules = true;
         }
-        else {
-            result.submoduleDirectories = null;
-        }
         core.debug(`submodules = ${result.submodules}`);
         core.debug(`recursive submodules = ${result.nestedSubmodules}`);
         core.debug(`submodule directories = ${result.submoduleDirectories}`);
diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts
index 9c3704348..bc73118a1 100644
--- a/src/git-command-manager.ts
+++ b/src/git-command-manager.ts
@@ -57,7 +57,7 @@ export interface IGitCommandManager {
   submoduleUpdate(
     fetchDepth: number,
     recursive: boolean,
-    submoduleDirectories: string[] | null
+    submoduleDirectories: string[]
   ): Promise<void>
   submoduleStatus(): Promise<boolean>
   tagExists(pattern: string): Promise<boolean>
@@ -416,35 +416,25 @@ class GitCommandManager {
   async submoduleUpdate(
     fetchDepth: number,
     recursive: boolean,
-    submoduleDirectories: string[] | null
+    submoduleDirectories: string[]
   ): Promise<void> {
-    if (submoduleDirectories) {
-      for (const submodule of submoduleDirectories) {
-        const args = ['-c', 'protocol.version=2']
-        args.push('submodule', 'update', '--init', '--force', submodule)
-        if (fetchDepth > 0) {
-          args.push(`--depth=${fetchDepth}`)
-        }
-
-        if (recursive) {
-          args.push('--recursive')
-        }
-
-        await this.execGit(args)
-      }
-    } else {
-      const args = ['-c', 'protocol.version=2']
-      args.push('submodule', 'update', '--init', '--force')
-      if (fetchDepth > 0) {
-        args.push(`--depth=${fetchDepth}`)
-      }
-
-      if (recursive) {
-        args.push('--recursive')
-      }
+    const args = ['-c', 'protocol.version=2']
+    args.push(
+      'submodule',
+      'update',
+      '--init',
+      '--force',
+      ...submoduleDirectories
+    )
+    if (fetchDepth > 0) {
+      args.push(`--depth=${fetchDepth}`)
+    }
 
-      await this.execGit(args)
+    if (recursive) {
+      args.push('--recursive')
     }
+
+    await this.execGit(args)
   }
 
   async submoduleStatus(): Promise<boolean> {
diff --git a/src/git-source-settings.ts b/src/git-source-settings.ts
index 15554896a..518e0bfc3 100644
--- a/src/git-source-settings.ts
+++ b/src/git-source-settings.ts
@@ -77,7 +77,7 @@ export interface IGitSourceSettings {
   /**
    * Indicates which submodule paths to checkout
    */
-  submoduleDirectories: string[] | null
+  submoduleDirectories: string[]
 
   /**
    * The auth token to use when fetching the repository
diff --git a/src/input-helper.ts b/src/input-helper.ts
index f367ce05e..5b2ff6c92 100644
--- a/src/input-helper.ts
+++ b/src/input-helper.ts
@@ -125,7 +125,7 @@ export async function getInputs(): Promise<IGitSourceSettings> {
   // Submodules
   result.submodules = false
   result.nestedSubmodules = false
-  result.submoduleDirectories = null
+  result.submoduleDirectories = []
   const submodulesString = (core.getInput('submodules') || '').toUpperCase()
   if (submodulesString == 'RECURSIVE') {
     result.submodules = true
@@ -138,8 +138,6 @@ export async function getInputs(): Promise<IGitSourceSettings> {
   if (submoduleDirectories.length > 0) {
     result.submoduleDirectories = submoduleDirectories
     if (!result.submodules) result.submodules = true
-  } else {
-    result.submoduleDirectories = null
   }
 
   core.debug(`submodules = ${result.submodules}`)