Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sparse checkouts #680

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ jobs:
- name: Verify submodules recursive
run: __test__/verify-submodules-recursive.sh

# Sparse checkout
- name: Sparse checkout
uses: ./
with:
ref: test-data/v2/sparse-checkout
path: sparse-checkout
sparse: |
dir2
dir3
sparse-cone: true
- name: Verify sparse checkout
run: __test__/verify-sparse-checkout.sh


# Basic checkout using REST API
- name: Remove basic
if: runner.os != 'windows'
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ Refer [here](https://github.com/actions/checkout/blob/v1/README.md) for previous
# Default: false
lfs: ''

# Do a sparse checkout on given patterns
# Default: null
sparse: ''

# Use cone pattern for sparse checkout
# Default: false
sparse-cone: ''

# Whether to checkout submodules: `true` to checkout submodules or `recursive` to
# recursively checkout submodules.
#
Expand Down
3 changes: 3 additions & 0 deletions __test__/git-auth-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ async function setup(testName: string): Promise<void> {
git.env[name] = value
}),
shaExists: jest.fn(),
sparseCheckout: jest.fn(),
submoduleForeach: jest.fn(async () => {
return ''
}),
Expand Down Expand Up @@ -773,6 +774,8 @@ async function setup(testName: string): Promise<void> {
repositoryName: 'my-repo',
repositoryOwner: 'my-org',
repositoryPath: '',
sparse: null,
sparseCone: false,
sshKey: sshPath ? 'some ssh private key' : '',
sshKnownHosts: '',
sshStrict: true,
Expand Down
1 change: 1 addition & 0 deletions __test__/git-directory-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ async function setup(testName: string): Promise<void> {
revParse: jest.fn(),
setEnvironmentVariable: jest.fn(),
shaExists: jest.fn(),
sparseCheckout: jest.fn(),
submoduleForeach: jest.fn(),
submoduleSync: jest.fn(),
submoduleUpdate: jest.fn(),
Expand Down
25 changes: 25 additions & 0 deletions __test__/verify-sparse-checkout.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

## REMOVE THIS
exit 0
## REMOVE THIS

jonnelaaglandwinder marked this conversation as resolved.
Show resolved Hide resolved
if [ ! -f "./sparse-checkout/root.txt" ]; then
echo "Expected file 'root.txt' to exist"
exit 1
fi

if [ -d "./sparse-checkout/dir1" ]; then
echo "Expected directory 'dir1' to not exist"
exit 1
fi

if [ ! -d "./sparse-checkout/dir2" ]; then
echo "Expected directory 'dir2' to exist"
exit 1
fi

if [ ! -d "./sparse-checkout/dir3" ]; then
echo "Expected directory 'dir3' to exist"
exit 1
fi
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ inputs:
lfs:
description: 'Whether to download Git-LFS files'
default: false
sparse:
description: 'Do a sparse checkout on given patterns'
default: null
sparse-cone:
description: 'Use cone pattern for sparse checkout'
default: false
submodules:
description: >
Whether to checkout submodules: `true` to checkout submodules or `recursive` to
Expand Down
27 changes: 27 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7076,6 +7076,17 @@ class GitCommandManager {
return output.exitCode === 0;
});
}
sparseCheckout(cone, paths) {
return __awaiter(this, void 0, void 0, function* () {
const args1 = ['sparse-checkout', 'init'];
if (cone) {
args1.push('--cone');
}
const args2 = ['sparse-checkout', 'set', '--', ...paths];
yield this.execGit(args1);
yield this.execGit(args2);
});
}
submoduleForeach(command, recursive) {
return __awaiter(this, void 0, void 0, function* () {
const args = ['submodule', 'foreach'];
Expand Down Expand Up @@ -7409,6 +7420,12 @@ function getSource(settings) {
yield git.lfsFetch(checkoutInfo.startPoint || checkoutInfo.ref);
core.endGroup();
}
// Sparse checkout
if (settings.sparse) {
core.startGroup('Sparse checkout');
yield git.sparseCheckout(settings.sparseCone, settings.sparse);
core.endGroup();
}
// Checkout
core.startGroup('Checking out the ref');
yield git.checkout(checkoutInfo.ref, checkoutInfo.startPoint);
Expand Down Expand Up @@ -17219,6 +17236,16 @@ function getInputs() {
// LFS
result.lfs = (core.getInput('lfs') || 'false').toUpperCase() === 'TRUE';
core.debug(`lfs = ${result.lfs}`);
if (core.getInput('sparse')) {
const paths = core
.getInput('sparse')
.trim()
.split(`\n`)
.map(s => s.trim());
result.sparse = paths;
}
result.sparseCone =
(core.getInput('sparse-cone') || 'false').toUpperCase() === 'TRUE';
// Submodules
result.submodules = false;
result.nestedSubmodules = false;
Expand Down
14 changes: 14 additions & 0 deletions src/git-command-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface IGitCommandManager {
revParse(ref: string): Promise<string>
setEnvironmentVariable(name: string, value: string): void
shaExists(sha: string): Promise<boolean>
sparseCheckout(cone: boolean, paths: string[]): Promise<void>
submoduleForeach(command: string, recursive: boolean): Promise<string>
submoduleSync(recursive: boolean): Promise<void>
submoduleUpdate(fetchDepth: number, recursive: boolean): Promise<void>
Expand Down Expand Up @@ -292,6 +293,19 @@ class GitCommandManager {
return output.exitCode === 0
}

async sparseCheckout(cone: boolean, paths: string[]): Promise<void> {
const args1 = ['sparse-checkout', 'init']

if (cone) {
args1.push('--cone')
}

const args2 = ['sparse-checkout', 'set', '--', ...paths]

await this.execGit(args1)
await this.execGit(args2)
}

async submoduleForeach(command: string, recursive: boolean): Promise<string> {
const args = ['submodule', 'foreach']
if (recursive) {
Expand Down
7 changes: 7 additions & 0 deletions src/git-source-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
core.endGroup()
}

// Sparse checkout
if (settings.sparse) {
core.startGroup('Sparse checkout')
await git.sparseCheckout(settings.sparseCone, settings.sparse)
core.endGroup()
}

// Checkout
core.startGroup('Checking out the ref')
await git.checkout(checkoutInfo.ref, checkoutInfo.startPoint)
Expand Down
10 changes: 10 additions & 0 deletions src/git-source-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,14 @@ export interface IGitSourceSettings {
* Organization ID for the currently running workflow (used for auth settings)
*/
workflowOrganizationId: number | undefined

/**
* Patterns to do a sparse checkout on
*/
sparse: string[] | null

/**
* Whether or not to use cone pattern on sparse checkout
*/
sparseCone: boolean
}
13 changes: 13 additions & 0 deletions src/input-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ export async function getInputs(): Promise<IGitSourceSettings> {
result.lfs = (core.getInput('lfs') || 'false').toUpperCase() === 'TRUE'
core.debug(`lfs = ${result.lfs}`)

if (core.getInput('sparse')) {
const paths = core
.getInput('sparse')
.trim()
.split(`\n`)
.map(s => s.trim())

result.sparse = paths
}

result.sparseCone =
(core.getInput('sparse-cone') || 'false').toUpperCase() === 'TRUE'

// Submodules
result.submodules = false
result.nestedSubmodules = false
Expand Down