diff --git a/action.yml b/action.yml index cab09eb..1a2ecfc 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 ab07524..132f34e 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 48f20da..92e9d00 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 2da5622..182c453 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 237b06a..e9a2d73 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) {