From 9f6bfbc6bc91046bde1b6b25ae631fb18d6532b8 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 2 May 2023 19:39:43 +0200 Subject: [PATCH 01/13] feat: added sparse-checkout option --- action.yml | 3 +++ src/git-command-manager.ts | 31 +++++++++++++++++++++++++------ src/git-source-provider.ts | 16 +++++++++++++--- src/git-source-settings.ts | 5 +++++ src/input-helper.ts | 7 +++++++ 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/action.yml b/action.yml index cab09ebd3..1a2ecfcb3 100644 --- a/action.yml +++ b/action.yml @@ -53,6 +53,9 @@ inputs: clean: description: 'Whether to execute `git clean -ffdx && git reset --hard HEAD` before fetching' default: true + sparse-checkout: + description: 'Do a sparse checkout on given patterns' + default: null fetch-depth: description: 'Number of commits to fetch. 0 indicates all history for all branches and tags.' default: 1 diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index ab07524e1..132f34e65 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -16,6 +16,7 @@ export interface IGitCommandManager { branchDelete(remote: boolean, branch: string): Promise branchExists(remote: boolean, pattern: string): Promise branchList(remote: boolean): Promise + sparseCheckout(sparseCheckout: string[]): Promise checkout(ref: string, startPoint: string): Promise checkoutDetach(): Promise config( @@ -25,7 +26,13 @@ export interface IGitCommandManager { add?: boolean ): Promise configExists(configKey: string, globalConfig?: boolean): Promise - fetch(refSpec: string[], fetchDepth?: number): Promise + fetch( + refSpec: string[], + options: { + filter?: string + fetchDepth?: number + } + ): Promise getDefaultBranch(repositoryUrl: string): Promise getWorkingDirectory(): string init(): Promise @@ -154,6 +161,10 @@ class GitCommandManager { return result } + async sparseCheckout(sparseCheckout: string[]): Promise { + await this.execGit(['sparse-checkout', 'set', ...sparseCheckout]) + } + async checkout(ref: string, startPoint: string): Promise { const args = ['checkout', '--progress', '--force'] if (startPoint) { @@ -202,15 +213,23 @@ class GitCommandManager { return output.exitCode === 0 } - async fetch(refSpec: string[], fetchDepth?: number): Promise { + async fetch( + refSpec: string[], + options: {filter?: string; fetchDepth?: number} + ): Promise { const args = ['-c', 'protocol.version=2', 'fetch'] if (!refSpec.some(x => x === refHelper.tagsRefSpec)) { args.push('--no-tags') } args.push('--prune', '--progress', '--no-recurse-submodules') - if (fetchDepth && fetchDepth > 0) { - args.push(`--depth=${fetchDepth}`) + + if (options.filter) { + args.push(`--filter=${options.filter}`) + } + + if (options.fetchDepth && options.fetchDepth > 0) { + args.push(`--depth=${options.fetchDepth}`) } else if ( fshelper.fileExistsSync( path.join(this.workingDirectory, '.git', 'shallow') @@ -289,8 +308,8 @@ class GitCommandManager { } async log1(format?: string): Promise { - var args = format ? ['log', '-1', format] : ['log', '-1'] - var silent = format ? false : true + const args = format ? ['log', '-1', format] : ['log', '-1'] + const silent = format ? false : true const output = await this.execGit(args, false, silent) return output.stdout } diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts index 48f20da28..92e9d005c 100644 --- a/src/git-source-provider.ts +++ b/src/git-source-provider.ts @@ -153,23 +153,26 @@ export async function getSource(settings: IGitSourceSettings): Promise { // Fetch core.startGroup('Fetching the repository') + const fetchOptions: {filter?: string; fetchDepth?: number} = {} + if (settings.sparseCheckout) fetchOptions.filter = 'blob:none' if (settings.fetchDepth <= 0) { // Fetch all branches and tags let refSpec = refHelper.getRefSpecForAllHistory( settings.ref, settings.commit ) - await git.fetch(refSpec) + await git.fetch(refSpec, fetchOptions) // When all history is fetched, the ref we're interested in may have moved to a different // commit (push or force push). If so, fetch again with a targeted refspec. if (!(await refHelper.testRef(git, settings.ref, settings.commit))) { refSpec = refHelper.getRefSpec(settings.ref, settings.commit) - await git.fetch(refSpec) + await git.fetch(refSpec, fetchOptions) } } else { + fetchOptions.fetchDepth = settings.fetchDepth const refSpec = refHelper.getRefSpec(settings.ref, settings.commit) - await git.fetch(refSpec, settings.fetchDepth) + await git.fetch(refSpec, fetchOptions) } core.endGroup() @@ -191,6 +194,13 @@ export async function getSource(settings: IGitSourceSettings): Promise { core.endGroup() } + // Sparse checkout + if (settings.sparseCheckout) { + core.startGroup('Setting up sparse checkout') + await git.sparseCheckout(settings.sparseCheckout) + core.endGroup() + } + // Checkout core.startGroup('Checking out the ref') await git.checkout(checkoutInfo.ref, checkoutInfo.startPoint) diff --git a/src/git-source-settings.ts b/src/git-source-settings.ts index 2da562266..182c4536c 100644 --- a/src/git-source-settings.ts +++ b/src/git-source-settings.ts @@ -29,6 +29,11 @@ export interface IGitSourceSettings { */ clean: boolean + /** + * The array of folders to make the sparse checkout + */ + sparseCheckout: string[] + /** * The depth when fetching */ diff --git a/src/input-helper.ts b/src/input-helper.ts index 237b06aff..e9a2d73d3 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -82,6 +82,13 @@ export async function getInputs(): Promise { result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE' core.debug(`clean = ${result.clean}`) + // Sparse checkout + const sparseCheckout = core.getMultilineInput('sparse-checkout') + if (sparseCheckout.length) { + result.sparseCheckout = sparseCheckout + core.debug(`sparse checkout = ${result.sparseCheckout}`) + } + // Fetch depth result.fetchDepth = Math.floor(Number(core.getInput('fetch-depth') || '1')) if (isNaN(result.fetchDepth) || result.fetchDepth < 0) { From 5bd25a58596ad1a1def63a3b6ad784c1d22a7dea Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 2 May 2023 19:41:00 +0200 Subject: [PATCH 02/13] feat: updated dist with build --- dist/index.js | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/dist/index.js b/dist/index.js index e6f5df8ff..c7aee5ba1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -574,6 +574,11 @@ class GitCommandManager { return result; }); } + sparseCheckout(sparseCheckout) { + return __awaiter(this, void 0, void 0, function* () { + yield this.execGit(['sparse-checkout', 'set', ...sparseCheckout]); + }); + } checkout(ref, startPoint) { return __awaiter(this, void 0, void 0, function* () { const args = ['checkout', '--progress', '--force']; @@ -615,15 +620,18 @@ class GitCommandManager { return output.exitCode === 0; }); } - fetch(refSpec, fetchDepth) { + fetch(refSpec, options) { return __awaiter(this, void 0, void 0, function* () { const args = ['-c', 'protocol.version=2', 'fetch']; if (!refSpec.some(x => x === refHelper.tagsRefSpec)) { args.push('--no-tags'); } args.push('--prune', '--progress', '--no-recurse-submodules'); - if (fetchDepth && fetchDepth > 0) { - args.push(`--depth=${fetchDepth}`); + if (options.filter) { + args.push(`--filter=${options.filter}`); + } + if (options.fetchDepth && options.fetchDepth > 0) { + args.push(`--depth=${options.fetchDepth}`); } else if (fshelper.fileExistsSync(path.join(this.workingDirectory, '.git', 'shallow'))) { args.push('--unshallow'); @@ -696,8 +704,8 @@ class GitCommandManager { } log1(format) { return __awaiter(this, void 0, void 0, function* () { - var args = format ? ['log', '-1', format] : ['log', '-1']; - var silent = format ? false : true; + const args = format ? ['log', '-1', format] : ['log', '-1']; + const silent = format ? false : true; const output = yield this.execGit(args, false, silent); return output.stdout; }); @@ -1210,20 +1218,24 @@ function getSource(settings) { } // Fetch core.startGroup('Fetching the repository'); + const fetchOptions = {}; + if (settings.sparseCheckout) + fetchOptions.filter = 'blob:none'; if (settings.fetchDepth <= 0) { // Fetch all branches and tags let refSpec = refHelper.getRefSpecForAllHistory(settings.ref, settings.commit); - yield git.fetch(refSpec); + yield git.fetch(refSpec, fetchOptions); // When all history is fetched, the ref we're interested in may have moved to a different // commit (push or force push). If so, fetch again with a targeted refspec. if (!(yield refHelper.testRef(git, settings.ref, settings.commit))) { refSpec = refHelper.getRefSpec(settings.ref, settings.commit); - yield git.fetch(refSpec); + yield git.fetch(refSpec, fetchOptions); } } else { + fetchOptions.fetchDepth = settings.fetchDepth; const refSpec = refHelper.getRefSpec(settings.ref, settings.commit); - yield git.fetch(refSpec, settings.fetchDepth); + yield git.fetch(refSpec, fetchOptions); } core.endGroup(); // Checkout info @@ -1238,6 +1250,12 @@ function getSource(settings) { yield git.lfsFetch(checkoutInfo.startPoint || checkoutInfo.ref); core.endGroup(); } + // Sparse checkout + if (settings.sparseCheckout) { + core.startGroup('Setting up sparse checkout'); + yield git.sparseCheckout(settings.sparseCheckout); + core.endGroup(); + } // Checkout core.startGroup('Checking out the ref'); yield git.checkout(checkoutInfo.ref, checkoutInfo.startPoint); @@ -1673,6 +1691,12 @@ function getInputs() { // Clean result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'; core.debug(`clean = ${result.clean}`); + // Sparse checkout + const sparseCheckout = core.getMultilineInput('sparse-checkout'); + if (sparseCheckout.length) { + result.sparseCheckout = sparseCheckout; + core.debug(`sparse checkout = ${result.sparseCheckout}`); + } // Fetch depth result.fetchDepth = Math.floor(Number(core.getInput('fetch-depth') || '1')); if (isNaN(result.fetchDepth) || result.fetchDepth < 0) { From d963058cca0b9eeca56d88fc64950b53a804feef Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 2 May 2023 19:41:38 +0200 Subject: [PATCH 03/13] feat: updated test for sparse-checkout --- __test__/git-auth-helper.test.ts | 2 ++ __test__/git-directory-helper.test.ts | 1 + __test__/input-helper.test.ts | 1 + 3 files changed, 4 insertions(+) diff --git a/__test__/git-auth-helper.test.ts b/__test__/git-auth-helper.test.ts index b58010b7c..f2cbfa502 100644 --- a/__test__/git-auth-helper.test.ts +++ b/__test__/git-auth-helper.test.ts @@ -727,6 +727,7 @@ async function setup(testName: string): Promise { branchDelete: jest.fn(), branchExists: jest.fn(), branchList: jest.fn(), + sparseCheckout: jest.fn(), checkout: jest.fn(), checkoutDetach: jest.fn(), config: jest.fn( @@ -800,6 +801,7 @@ async function setup(testName: string): Promise { authToken: 'some auth token', clean: true, commit: '', + sparseCheckout: [], fetchDepth: 1, lfs: false, submodules: false, diff --git a/__test__/git-directory-helper.test.ts b/__test__/git-directory-helper.test.ts index 02118ae48..8b05606df 100644 --- a/__test__/git-directory-helper.test.ts +++ b/__test__/git-directory-helper.test.ts @@ -462,6 +462,7 @@ async function setup(testName: string): Promise { branchList: jest.fn(async () => { return [] }), + sparseCheckout: jest.fn(), checkout: jest.fn(), checkoutDetach: jest.fn(), config: jest.fn(), diff --git a/__test__/input-helper.test.ts b/__test__/input-helper.test.ts index 1a8e5c96d..6d7421b4d 100644 --- a/__test__/input-helper.test.ts +++ b/__test__/input-helper.test.ts @@ -79,6 +79,7 @@ describe('input-helper tests', () => { expect(settings.clean).toBe(true) expect(settings.commit).toBeTruthy() expect(settings.commit).toBe('1234567890123456789012345678901234567890') + expect(settings.sparseCheckout).toBe(undefined) expect(settings.fetchDepth).toBe(1) expect(settings.lfs).toBe(false) expect(settings.ref).toBe('refs/heads/some-ref') From 360aefd1c123dd81410df28df0b66332a4c07a0a Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 2 May 2023 19:41:51 +0200 Subject: [PATCH 04/13] feat: added test for sparse-checkout --- .github/workflows/test.yml | 22 ++++++++++++- __test__/verify-sparse-checkout-basic.sh | 23 ++++++++++++++ __test__/verify-sparse-checkout.sh | 40 ++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100755 __test__/verify-sparse-checkout-basic.sh create mode 100755 __test__/verify-sparse-checkout.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c28e77194..632f1cbcc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -113,6 +113,26 @@ jobs: - name: Verify submodules recursive run: __test__/verify-submodules-recursive.sh + # Sparse checkout basic example + - name: Sparse checkout basic example + uses: ./ + with: + sparse-checkout: . + - name: Verify sparse checkout basic + run: __test__/verify-sparse-checkout-basic.sh + + # Sparse checkout example + - name: Sparse checkout example + uses: ./ + with: + sparse-checkout: | + .github + src + - name: Verify sparse checkout basic + run: __test__/verify-sparse-checkout-basic.sh + - name: Verify sparse checkout example + run: __test__/verify-sparse-checkout.sh + # Basic checkout using REST API - name: Remove basic if: runner.os != 'windows' @@ -205,7 +225,7 @@ jobs: path: basic - name: Verify basic run: __test__/verify-basic.sh --archive - + test-git-container: runs-on: ubuntu-latest container: bitnami/git:latest diff --git a/__test__/verify-sparse-checkout-basic.sh b/__test__/verify-sparse-checkout-basic.sh new file mode 100755 index 000000000..163db29b4 --- /dev/null +++ b/__test__/verify-sparse-checkout-basic.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +SPARSE=$(git sparse-checkout list) + +if [ "$?" != "0" ]; then + echo "Failed to validate sparse-checkout" + exit 1 +fi + +# Check that sparse-checkout list is not empty +if [ -z "$SPARSE" ]; then + echo "Expected sparse-checkout list to not be empty" + exit 1 +fi + +# Check that all folders from sparse-checkout exists +for pattern in $(git sparse-checkout list) +do + if [ ! -d "$pattern" ]; then + echo "Expected directory '$pattern' to exist" + exit 1 + fi +done \ No newline at end of file diff --git a/__test__/verify-sparse-checkout.sh b/__test__/verify-sparse-checkout.sh new file mode 100755 index 000000000..69d1676b5 --- /dev/null +++ b/__test__/verify-sparse-checkout.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# Check that only sparse-checkout folders has been fetched +for pattern in $(git ls-tree --name-only HEAD) +do + if [ -d "$pattern" ]; then + if [[ "$pattern" != ".github" && "$pattern" != "src" ]]; then + echo "Expected directory '$pattern' to not exist" + exit 1 + fi + fi +done + +# Check that .github and its childrens has been fetched correctly +if [ ! -d "./.github" ]; then + echo "Expected directory '.github' to exist" + exit 1 +fi + +for file in $(git ls-tree -r --name-only HEAD .github) +do + if [ ! -f "$file" ]; then + echo "Expected file '$file' to exist" + exit 1 + fi +done + +# Check that src and its childrens has been fetched correctly +if [ ! -d "./src" ]; then + echo "Expected directory 'src' to exist" + exit 1 +fi + +for file in $(git ls-tree -r --name-only HEAD src) +do + if [ ! -f "$file" ]; then + echo "Expected file '$file' to exist" + exit 1 + fi +done \ No newline at end of file From f6aa55bb5883a6a68f74510bfb79fc2bdf26e59c Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 2 May 2023 19:53:43 +0200 Subject: [PATCH 05/13] feat: README.md updated with sparse-checkout option --- README.md | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8fe140f55..f1682879b 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,10 @@ When Git 2.18 or higher is not in your PATH, falls back to the REST API to downl # Default: true clean: '' + # Do a sparse checkout on given patterns (each pattern should be sepparated with new lines). + # Default: null + sparse-checkout: '' + # Number of commits to fetch. 0 indicates all history for all branches and tags. # Default: 1 fetch-depth: '' @@ -106,15 +110,35 @@ When Git 2.18 or higher is not in your PATH, falls back to the REST API to downl # Scenarios -- [Fetch all history for all tags and branches](#Fetch-all-history-for-all-tags-and-branches) -- [Checkout a different branch](#Checkout-a-different-branch) -- [Checkout HEAD^](#Checkout-HEAD) -- [Checkout multiple repos (side by side)](#Checkout-multiple-repos-side-by-side) -- [Checkout multiple repos (nested)](#Checkout-multiple-repos-nested) -- [Checkout multiple repos (private)](#Checkout-multiple-repos-private) -- [Checkout pull request HEAD commit instead of merge commit](#Checkout-pull-request-HEAD-commit-instead-of-merge-commit) -- [Checkout pull request on closed event](#Checkout-pull-request-on-closed-event) -- [Push a commit using the built-in token](#Push-a-commit-using-the-built-in-token) +- [Fetch only the root files](#fetch-only-the-root-files) +- [Fetch only the root files and `.github` and `src` folder](#fetch-only-the-root-files-and-github-and-src-folder) +- [Fetch all history for all tags and branches](#fetch-all-history-for-all-tags-and-branches) +- [Checkout a different branch](#checkout-a-different-branch) +- [Checkout HEAD^](#checkout-head) +- [Checkout multiple repos (side by side)](#checkout-multiple-repos-side-by-side) +- [Checkout multiple repos (nested)](#checkout-multiple-repos-nested) +- [Checkout multiple repos (private)](#checkout-multiple-repos-private) +- [Checkout pull request HEAD commit instead of merge commit](#checkout-pull-request-head-commit-instead-of-merge-commit) +- [Checkout pull request on closed event](#checkout-pull-request-on-closed-event) +- [Push a commit using the built-in token](#push-a-commit-using-the-built-in-token) + +## Fetch only the root files + +```yaml +- uses: actions/checkout@v3 + with: + sparse-checkout: . +``` + +## Fetch only the root files and `.github` and `src` folder + +```yaml +- uses: actions/checkout@v3 + with: + sparse-checkout: | + .github + src +``` ## Fetch all history for all tags and branches From c6f93f590913c18592d6e3e6e5e283661d7555c9 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 2 May 2023 20:03:55 +0200 Subject: [PATCH 06/13] feat: README.md updated with correct lint --- README.md | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index f1682879b..9f622fb53 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,8 @@ When Git 2.18 or higher is not in your PATH, falls back to the REST API to downl # Default: true clean: '' - # Do a sparse checkout on given patterns (each pattern should be sepparated with new lines). + # Do a sparse checkout on given patterns + # Each pattern should be sepparated with new lines # Default: null sparse-checkout: '' @@ -110,17 +111,22 @@ When Git 2.18 or higher is not in your PATH, falls back to the REST API to downl # Scenarios -- [Fetch only the root files](#fetch-only-the-root-files) -- [Fetch only the root files and `.github` and `src` folder](#fetch-only-the-root-files-and-github-and-src-folder) -- [Fetch all history for all tags and branches](#fetch-all-history-for-all-tags-and-branches) -- [Checkout a different branch](#checkout-a-different-branch) -- [Checkout HEAD^](#checkout-head) -- [Checkout multiple repos (side by side)](#checkout-multiple-repos-side-by-side) -- [Checkout multiple repos (nested)](#checkout-multiple-repos-nested) -- [Checkout multiple repos (private)](#checkout-multiple-repos-private) -- [Checkout pull request HEAD commit instead of merge commit](#checkout-pull-request-head-commit-instead-of-merge-commit) -- [Checkout pull request on closed event](#checkout-pull-request-on-closed-event) -- [Push a commit using the built-in token](#push-a-commit-using-the-built-in-token) +- [Checkout V3](#checkout-v3) +- [What's new](#whats-new) +- [Usage](#usage) +- [Scenarios](#scenarios) + - [Fetch only the root files](#fetch-only-the-root-files) + - [Fetch only the root files and `.github` and `src` folder](#fetch-only-the-root-files-and-github-and-src-folder) + - [Fetch all history for all tags and branches](#fetch-all-history-for-all-tags-and-branches) + - [Checkout a different branch](#checkout-a-different-branch) + - [Checkout HEAD^](#checkout-head) + - [Checkout multiple repos (side by side)](#checkout-multiple-repos-side-by-side) + - [Checkout multiple repos (nested)](#checkout-multiple-repos-nested) + - [Checkout multiple repos (private)](#checkout-multiple-repos-private) + - [Checkout pull request HEAD commit instead of merge commit](#checkout-pull-request-head-commit-instead-of-merge-commit) + - [Checkout pull request on closed event](#checkout-pull-request-on-closed-event) + - [Push a commit using the built-in token](#push-a-commit-using-the-built-in-token) +- [License](#license) ## Fetch only the root files From 55b0bfb9c850818c73fc67177f6fed504153fc3f Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 2 May 2023 20:14:03 +0200 Subject: [PATCH 07/13] feat: added __test__ as sparse-checkout directory in order to be able to have the test scripts --- .github/workflows/test.yml | 3 ++- __test__/verify-sparse-checkout.sh | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 632f1cbcc..518771a28 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -117,7 +117,7 @@ jobs: - name: Sparse checkout basic example uses: ./ with: - sparse-checkout: . + sparse-checkout: __test__ - name: Verify sparse checkout basic run: __test__/verify-sparse-checkout-basic.sh @@ -126,6 +126,7 @@ jobs: uses: ./ with: sparse-checkout: | + __test__ .github src - name: Verify sparse checkout basic diff --git a/__test__/verify-sparse-checkout.sh b/__test__/verify-sparse-checkout.sh index 69d1676b5..a41887874 100755 --- a/__test__/verify-sparse-checkout.sh +++ b/__test__/verify-sparse-checkout.sh @@ -4,13 +4,27 @@ for pattern in $(git ls-tree --name-only HEAD) do if [ -d "$pattern" ]; then - if [[ "$pattern" != ".github" && "$pattern" != "src" ]]; then + if [[ "$pattern" != "__test__" && "$pattern" != ".github" && "$pattern" != "src" ]]; then echo "Expected directory '$pattern' to not exist" exit 1 fi fi done +# Check that .github and its childrens has been fetched correctly +if [ ! -d "./__test__" ]; then + echo "Expected directory '__test__' to exist" + exit 1 +fi + +for file in $(git ls-tree -r --name-only HEAD __test__) +do + if [ ! -f "$file" ]; then + echo "Expected file '$file' to exist" + exit 1 + fi +done + # Check that .github and its childrens has been fetched correctly if [ ! -d "./.github" ]; then echo "Expected directory '.github' to exist" From 18e5a0dd6234fd5e0e742450bba015560cae341a Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 2 May 2023 20:40:13 +0200 Subject: [PATCH 08/13] feat: action.yml and README.md updated with correct lint --- README.md | 4 ++-- action.yml | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9f622fb53..3f413f7cd 100644 --- a/README.md +++ b/README.md @@ -74,8 +74,8 @@ When Git 2.18 or higher is not in your PATH, falls back to the REST API to downl # Default: true clean: '' - # Do a sparse checkout on given patterns - # Each pattern should be sepparated with new lines + # Do a sparse checkout on given patterns. Each pattern should be sepparated with + # new lines # Default: null sparse-checkout: '' diff --git a/action.yml b/action.yml index 1a2ecfcb3..bb7451ed2 100644 --- a/action.yml +++ b/action.yml @@ -54,7 +54,9 @@ inputs: description: 'Whether to execute `git clean -ffdx && git reset --hard HEAD` before fetching' default: true sparse-checkout: - description: 'Do a sparse checkout on given patterns' + description: > + Do a sparse checkout on given patterns. + Each pattern should be sepparated with new lines default: null fetch-depth: description: 'Number of commits to fetch. 0 indicates all history for all branches and tags.' From e12b86ed416ffa3e0c5e065186d05bb10b04a523 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 2 May 2023 20:40:47 +0200 Subject: [PATCH 09/13] feat: updated test to make all checks in only one check --- .github/workflows/test.yml | 14 +++----------- __test__/verify-sparse-checkout.sh | 10 +++++----- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 518771a28..75e60071a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -113,22 +113,14 @@ jobs: - name: Verify submodules recursive run: __test__/verify-submodules-recursive.sh - # Sparse checkout basic example - - name: Sparse checkout basic example - uses: ./ - with: - sparse-checkout: __test__ - - name: Verify sparse checkout basic - run: __test__/verify-sparse-checkout-basic.sh - - # Sparse checkout example - - name: Sparse checkout example + # Sparse checkout + - name: Sparse checkout uses: ./ with: sparse-checkout: | __test__ .github - src + dist - name: Verify sparse checkout basic run: __test__/verify-sparse-checkout-basic.sh - name: Verify sparse checkout example diff --git a/__test__/verify-sparse-checkout.sh b/__test__/verify-sparse-checkout.sh index a41887874..64b26ca99 100755 --- a/__test__/verify-sparse-checkout.sh +++ b/__test__/verify-sparse-checkout.sh @@ -4,7 +4,7 @@ for pattern in $(git ls-tree --name-only HEAD) do if [ -d "$pattern" ]; then - if [[ "$pattern" != "__test__" && "$pattern" != ".github" && "$pattern" != "src" ]]; then + if [[ "$pattern" != "__test__" && "$pattern" != ".github" && "$pattern" != "dist" ]]; then echo "Expected directory '$pattern' to not exist" exit 1 fi @@ -39,13 +39,13 @@ do fi done -# Check that src and its childrens has been fetched correctly -if [ ! -d "./src" ]; then - echo "Expected directory 'src' to exist" +# Check that dist and its childrens has been fetched correctly +if [ ! -d "./dist" ]; then + echo "Expected directory 'dist' to exist" exit 1 fi -for file in $(git ls-tree -r --name-only HEAD src) +for file in $(git ls-tree -r --name-only HEAD dist) do if [ ! -f "$file" ]; then echo "Expected file '$file' to exist" From 15b0c61ab07f97e88b7ed5593cef29dd0d629a38 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 2 May 2023 20:42:00 +0200 Subject: [PATCH 10/13] feat: recovered old README.md structure --- README.md | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 3f413f7cd..f46b30af4 100644 --- a/README.md +++ b/README.md @@ -111,22 +111,17 @@ When Git 2.18 or higher is not in your PATH, falls back to the REST API to downl # Scenarios -- [Checkout V3](#checkout-v3) -- [What's new](#whats-new) -- [Usage](#usage) -- [Scenarios](#scenarios) - - [Fetch only the root files](#fetch-only-the-root-files) - - [Fetch only the root files and `.github` and `src` folder](#fetch-only-the-root-files-and-github-and-src-folder) - - [Fetch all history for all tags and branches](#fetch-all-history-for-all-tags-and-branches) - - [Checkout a different branch](#checkout-a-different-branch) - - [Checkout HEAD^](#checkout-head) - - [Checkout multiple repos (side by side)](#checkout-multiple-repos-side-by-side) - - [Checkout multiple repos (nested)](#checkout-multiple-repos-nested) - - [Checkout multiple repos (private)](#checkout-multiple-repos-private) - - [Checkout pull request HEAD commit instead of merge commit](#checkout-pull-request-head-commit-instead-of-merge-commit) - - [Checkout pull request on closed event](#checkout-pull-request-on-closed-event) - - [Push a commit using the built-in token](#push-a-commit-using-the-built-in-token) -- [License](#license) +- [Fetch only the root files](#fetch-only-the-root-files) +- [Fetch only the root files and `.github` and `src` folder](#fetch-only-the-root-files-and-github-and-src-folder) +- [Fetch all history for all tags and branches](#fetch-all-history-for-all-tags-and-branches) +- [Checkout a different branch](#checkout-a-different-branch) +- [Checkout HEAD^](#checkout-head) +- [Checkout multiple repos (side by side)](#checkout-multiple-repos-side-by-side) +- [Checkout multiple repos (nested)](#checkout-multiple-repos-nested) +- [Checkout multiple repos (private)](#checkout-multiple-repos-private) +- [Checkout pull request HEAD commit instead of merge commit](#checkout-pull-request-head-commit-instead-of-merge-commit) +- [Checkout pull request on closed event](#checkout-pull-request-on-closed-event) +- [Push a commit using the built-in token](#push-a-commit-using-the-built-in-token) ## Fetch only the root files From fe6a5ed262ab00f1f4e0ead2e035bfb8dbbf7062 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 2 May 2023 20:47:28 +0200 Subject: [PATCH 11/13] feat: updated sparse-checkout test position to avoid errors with default test behaviour --- .github/workflows/test.yml | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 75e60071a..83b343ece 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -72,6 +72,20 @@ jobs: shell: bash run: __test__/verify-side-by-side.sh + # Sparse checkout + - name: Sparse checkout + uses: ./ + with: + sparse-checkout: | + __test__ + .github + dist + + - name: Verify sparse checkout basic + run: __test__/verify-sparse-checkout-basic.sh + - name: Verify sparse checkout example + run: __test__/verify-sparse-checkout.sh + # LFS - name: Checkout LFS uses: ./ @@ -113,19 +127,6 @@ jobs: - name: Verify submodules recursive run: __test__/verify-submodules-recursive.sh - # Sparse checkout - - name: Sparse checkout - uses: ./ - with: - sparse-checkout: | - __test__ - .github - dist - - name: Verify sparse checkout basic - run: __test__/verify-sparse-checkout-basic.sh - - name: Verify sparse checkout example - run: __test__/verify-sparse-checkout.sh - # Basic checkout using REST API - name: Remove basic if: runner.os != 'windows' From a910f4bca0f56c5db319dc313beac19addcebb2d Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 2 May 2023 20:52:49 +0200 Subject: [PATCH 12/13] feat: added path for sparse-checkout test --- .github/workflows/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 83b343ece..590da62ff 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -80,6 +80,7 @@ jobs: __test__ .github dist + path: sparse-checkout - name: Verify sparse checkout basic run: __test__/verify-sparse-checkout-basic.sh From eea6d0effb0ad7b5463639859f5810d7bbb2908f Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 2 May 2023 21:08:06 +0200 Subject: [PATCH 13/13] feat: refactor on sparse-checkout test --- __test__/verify-sparse-checkout-basic.sh | 9 ++++ __test__/verify-sparse-checkout.sh | 69 ++++++++++-------------- 2 files changed, 37 insertions(+), 41 deletions(-) diff --git a/__test__/verify-sparse-checkout-basic.sh b/__test__/verify-sparse-checkout-basic.sh index 163db29b4..f8085f40d 100755 --- a/__test__/verify-sparse-checkout-basic.sh +++ b/__test__/verify-sparse-checkout-basic.sh @@ -1,5 +1,14 @@ #!/bin/bash +# Verify .git folder +if [ ! -d "./sparse-checkout/.git" ]; then + echo "Expected ./sparse-checkout/.git folder to exist" + exit 1 +fi + +# Verify sparse-checkout basic +cd sparse-checkout + SPARSE=$(git sparse-checkout list) if [ "$?" != "0" ]; then diff --git a/__test__/verify-sparse-checkout.sh b/__test__/verify-sparse-checkout.sh index 64b26ca99..b0dd06000 100755 --- a/__test__/verify-sparse-checkout.sh +++ b/__test__/verify-sparse-checkout.sh @@ -1,54 +1,41 @@ #!/bin/bash -# Check that only sparse-checkout folders has been fetched -for pattern in $(git ls-tree --name-only HEAD) -do - if [ -d "$pattern" ]; then - if [[ "$pattern" != "__test__" && "$pattern" != ".github" && "$pattern" != "dist" ]]; then - echo "Expected directory '$pattern' to not exist" - exit 1 - fi - fi -done - -# Check that .github and its childrens has been fetched correctly -if [ ! -d "./__test__" ]; then - echo "Expected directory '__test__' to exist" +# Verify .git folder +if [ ! -d "./sparse-checkout/.git" ]; then + echo "Expected ./sparse-checkout/.git folder to exist" exit 1 fi -for file in $(git ls-tree -r --name-only HEAD __test__) -do - if [ ! -f "$file" ]; then - echo "Expected file '$file' to exist" - exit 1 - fi -done - -# Check that .github and its childrens has been fetched correctly -if [ ! -d "./.github" ]; then - echo "Expected directory '.github' to exist" - exit 1 -fi +# Verify sparse-checkout +cd sparse-checkout -for file in $(git ls-tree -r --name-only HEAD .github) -do - if [ ! -f "$file" ]; then - echo "Expected file '$file' to exist" +checkSparse () { + if [ ! -d "./$1" ]; then + echo "Expected directory '$1' to exist" exit 1 fi -done -# Check that dist and its childrens has been fetched correctly -if [ ! -d "./dist" ]; then - echo "Expected directory 'dist' to exist" - exit 1 -fi + for file in $(git ls-tree -r --name-only HEAD $1) + do + if [ ! -f "$file" ]; then + echo "Expected file '$file' to exist" + exit 1 + fi + done +} -for file in $(git ls-tree -r --name-only HEAD dist) +# Check that all folders and its childrens has been fetched correctly +checkSparse __test__ +checkSparse .github +checkSparse dist + +# Check that only sparse-checkout folders has been fetched +for pattern in $(git ls-tree --name-only HEAD) do - if [ ! -f "$file" ]; then - echo "Expected file '$file' to exist" - exit 1 + if [ -d "$pattern" ]; then + if [[ "$pattern" != "__test__" && "$pattern" != ".github" && "$pattern" != "dist" ]]; then + echo "Expected directory '$pattern' to not exist" + exit 1 + fi fi done \ No newline at end of file