From ba4104a5de1164f91dfe759a7306ea9c788ee0b5 Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Tue, 27 Aug 2024 09:55:59 +0200 Subject: [PATCH] Add string[] option to submodules Allows checking out only specific submodules instead of all --- .github/workflows/test.yml | 10 +++++++++ README.md | 3 ++- action.yml | 1 + dist/index.js | 33 +++++++++++++++++++-------- src/git-command-manager.ts | 46 +++++++++++++++++++++++++++++--------- src/git-source-provider.ts | 6 ++++- src/git-source-settings.ts | 2 +- 7 files changed, 78 insertions(+), 23 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1ef3c313e..edf36a877 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -154,6 +154,16 @@ jobs: submodules: true - name: Verify submodules true run: __test__/verify-submodules-true.sh + + # Submodules limited + - name: Checkout submodules true + uses: ./ + with: + ref: test-data/v2/submodule-ssh-url + path: submodules-true + submodules: submodule-level-1 + - name: Verify submodules true + run: __test__/verify-submodules-true.sh # Submodules recursive - name: Checkout submodules recursive diff --git a/README.md b/README.md index 9b6176d94..1596dcb98 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,8 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/ lfs: '' # Whether to checkout submodules: `true` to checkout submodules or `recursive` to - # recursively checkout submodules. + # recursively checkout submodules. Provide a list to specify which submodules to + # check out. # # When the `ssh-key` input is not provided, SSH URLs beginning with # `git@github.com:` are converted to HTTPS. diff --git a/action.yml b/action.yml index 75d5ae2d8..2a018f7b0 100644 --- a/action.yml +++ b/action.yml @@ -87,6 +87,7 @@ inputs: description: > Whether to checkout submodules: `true` to checkout submodules or `recursive` to recursively checkout submodules. + Provide a list to specify which submodules to check out. When the `ssh-key` input is not provided, SSH URLs beginning with `git@github.com:` are diff --git a/dist/index.js b/dist/index.js index 9d959a9ee..1edcad5c1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -795,17 +795,32 @@ class GitCommandManager { yield this.execGit(args); }); } - submoduleUpdate(fetchDepth, recursive) { + submoduleUpdate(fetchDepth, recursive, submodules) { return __awaiter(this, void 0, void 0, function* () { - const args = ['-c', 'protocol.version=2']; - args.push('submodule', 'update', '--init', '--force'); - if (fetchDepth > 0) { - args.push(`--depth=${fetchDepth}`); + if (Array.isArray(submodules)) { + for (const submodule of submodules) { + 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); + } } - if (recursive) { - args.push('--recursive'); + 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); } - yield this.execGit(args); }); } submoduleStatus() { @@ -1342,7 +1357,7 @@ function getSource(settings) { // Checkout submodules core.startGroup('Fetching submodules'); yield git.submoduleSync(settings.nestedSubmodules); - yield git.submoduleUpdate(settings.fetchDepth, settings.nestedSubmodules); + yield git.submoduleUpdate(settings.fetchDepth, settings.nestedSubmodules, settings.submodules); 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 8e42a387f..0dc46f1cf 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -54,7 +54,11 @@ export interface IGitCommandManager { shaExists(sha: string): Promise submoduleForeach(command: string, recursive: boolean): Promise submoduleSync(recursive: boolean): Promise - submoduleUpdate(fetchDepth: number, recursive: boolean): Promise + submoduleUpdate( + fetchDepth: number, + recursive: boolean, + submodules: boolean | string[] + ): Promise submoduleStatus(): Promise tagExists(pattern: string): Promise tryClean(): Promise @@ -409,18 +413,38 @@ class GitCommandManager { await this.execGit(args) } - async submoduleUpdate(fetchDepth: number, recursive: boolean): Promise { - const args = ['-c', 'protocol.version=2'] - args.push('submodule', 'update', '--init', '--force') - if (fetchDepth > 0) { - args.push(`--depth=${fetchDepth}`) - } + async submoduleUpdate( + fetchDepth: number, + recursive: boolean, + submodules: boolean | string[] + ): Promise { + if (Array.isArray(submodules)) { + for (const submodule of submodules) { + 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') - } + if (recursive) { + args.push('--recursive') + } - await this.execGit(args) + 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') + } + + await this.execGit(args) + } } async submoduleStatus(): Promise { diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts index f723d94f4..391c47b60 100644 --- a/src/git-source-provider.ts +++ b/src/git-source-provider.ts @@ -242,7 +242,11 @@ export async function getSource(settings: IGitSourceSettings): Promise { // Checkout submodules core.startGroup('Fetching submodules') await git.submoduleSync(settings.nestedSubmodules) - await git.submoduleUpdate(settings.fetchDepth, settings.nestedSubmodules) + await git.submoduleUpdate( + settings.fetchDepth, + settings.nestedSubmodules, + settings.submodules + ) await git.submoduleForeach( 'git config --local gc.auto 0', settings.nestedSubmodules diff --git a/src/git-source-settings.ts b/src/git-source-settings.ts index 4e41ac302..062f9f8dd 100644 --- a/src/git-source-settings.ts +++ b/src/git-source-settings.ts @@ -67,7 +67,7 @@ export interface IGitSourceSettings { /** * Indicates whether to checkout submodules */ - submodules: boolean + submodules: boolean | string[] /** * Indicates whether to recursively checkout submodules