Skip to content

Commit

Permalink
Add string[] option to submodules
Browse files Browse the repository at this point in the history
Allows checking out only specific submodules instead of all
  • Loading branch information
Maddimax committed Aug 27, 2024
1 parent 9a9194f commit ba4104a
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 23 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
# `[email protected]:` are converted to HTTPS.
Expand Down
1 change: 1 addition & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 `[email protected]:` are
Expand Down
33 changes: 24 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
46 changes: 35 additions & 11 deletions src/git-command-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ export interface IGitCommandManager {
shaExists(sha: string): Promise<boolean>
submoduleForeach(command: string, recursive: boolean): Promise<string>
submoduleSync(recursive: boolean): Promise<void>
submoduleUpdate(fetchDepth: number, recursive: boolean): Promise<void>
submoduleUpdate(
fetchDepth: number,
recursive: boolean,
submodules: boolean | string[]
): Promise<void>
submoduleStatus(): Promise<boolean>
tagExists(pattern: string): Promise<boolean>
tryClean(): Promise<boolean>
Expand Down Expand Up @@ -409,18 +413,38 @@ class GitCommandManager {
await this.execGit(args)
}

async submoduleUpdate(fetchDepth: number, recursive: boolean): Promise<void> {
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<void> {
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<boolean> {
Expand Down
6 changes: 5 additions & 1 deletion src/git-source-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,11 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
// 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
Expand Down
2 changes: 1 addition & 1 deletion src/git-source-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface IGitSourceSettings {
/**
* Indicates whether to checkout submodules
*/
submodules: boolean
submodules: boolean | string[]

/**
* Indicates whether to recursively checkout submodules
Expand Down

0 comments on commit ba4104a

Please sign in to comment.