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 all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 @@ -84,6 +84,14 @@ When Git 2.18 or higher is not in your PATH, falls back to the REST API to downl
# 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 @@ -766,6 +766,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 @@ -806,6 +807,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
21 changes: 21 additions & 0 deletions __test__/verify-sparse-checkout.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

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 @@ -7569,6 +7569,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 @@ -18456,6 +18467,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 Expand Up @@ -31928,6 +31949,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
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 @@ -191,6 +191,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 @@ -88,4 +88,14 @@ export interface IGitSourceSettings {
* User override on the GitHub Server/Host URL that hosts the repository to be cloned
*/
githubServerUrl: string | 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