diff options
Diffstat (limited to 'src/git-command-manager.ts')
| -rw-r--r-- | src/git-command-manager.ts | 78 |
1 files changed, 69 insertions, 9 deletions
diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index ab07524..e684dba 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -1,5 +1,6 @@ import * as core from '@actions/core' import * as exec from '@actions/exec' +import * as fs from 'fs' import * as fshelper from './fs-helper' import * as io from '@actions/io' import * as path from 'path' @@ -16,6 +17,8 @@ export interface IGitCommandManager { branchDelete(remote: boolean, branch: string): Promise<void> branchExists(remote: boolean, pattern: string): Promise<boolean> branchList(remote: boolean): Promise<string[]> + sparseCheckout(sparseCheckout: string[]): Promise<void> + sparseCheckoutNonConeMode(sparseCheckout: string[]): Promise<void> checkout(ref: string, startPoint: string): Promise<void> checkoutDetach(): Promise<void> config( @@ -25,7 +28,13 @@ export interface IGitCommandManager { add?: boolean ): Promise<void> configExists(configKey: string, globalConfig?: boolean): Promise<boolean> - fetch(refSpec: string[], fetchDepth?: number): Promise<void> + fetch( + refSpec: string[], + options: { + filter?: string + fetchDepth?: number + } + ): Promise<void> getDefaultBranch(repositoryUrl: string): Promise<string> getWorkingDirectory(): string init(): Promise<void> @@ -52,9 +61,14 @@ export interface IGitCommandManager { export async function createCommandManager( workingDirectory: string, - lfs: boolean + lfs: boolean, + doSparseCheckout: boolean ): Promise<IGitCommandManager> { - return await GitCommandManager.createCommandManager(workingDirectory, lfs) + return await GitCommandManager.createCommandManager( + workingDirectory, + lfs, + doSparseCheckout + ) } class GitCommandManager { @@ -64,6 +78,7 @@ class GitCommandManager { } private gitPath = '' private lfs = false + private doSparseCheckout = false private workingDirectory = '' // Private constructor; use createCommandManager() @@ -154,6 +169,27 @@ class GitCommandManager { return result } + async sparseCheckout(sparseCheckout: string[]): Promise<void> { + await this.execGit(['sparse-checkout', 'set', ...sparseCheckout]) + } + + async sparseCheckoutNonConeMode(sparseCheckout: string[]): Promise<void> { + await this.execGit(['config', 'core.sparseCheckout', 'true']) + const output = await this.execGit([ + 'rev-parse', + '--git-path', + 'info/sparse-checkout' + ]) + const sparseCheckoutPath = path.join( + this.workingDirectory, + output.stdout.trimRight() + ) + await fs.promises.appendFile( + sparseCheckoutPath, + `\n${sparseCheckout.join('\n')}\n` + ) + } + async checkout(ref: string, startPoint: string): Promise<void> { const args = ['checkout', '--progress', '--force'] if (startPoint) { @@ -202,15 +238,23 @@ class GitCommandManager { return output.exitCode === 0 } - async fetch(refSpec: string[], fetchDepth?: number): Promise<void> { + async fetch( + refSpec: string[], + options: {filter?: string; fetchDepth?: number} + ): Promise<void> { 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') @@ -423,10 +467,15 @@ class GitCommandManager { static async createCommandManager( workingDirectory: string, - lfs: boolean + lfs: boolean, + doSparseCheckout: boolean ): Promise<GitCommandManager> { const result = new GitCommandManager() - await result.initializeCommandManager(workingDirectory, lfs) + await result.initializeCommandManager( + workingDirectory, + lfs, + doSparseCheckout + ) return result } @@ -476,7 +525,8 @@ class GitCommandManager { private async initializeCommandManager( workingDirectory: string, - lfs: boolean + lfs: boolean, + doSparseCheckout: boolean ): Promise<void> { this.workingDirectory = workingDirectory @@ -539,6 +589,16 @@ class GitCommandManager { } } + this.doSparseCheckout = doSparseCheckout + if (this.doSparseCheckout) { + // The `git sparse-checkout` command was introduced in Git v2.25.0 + const minimumGitSparseCheckoutVersion = new GitVersion('2.25') + if (!gitVersion.checkMinimum(minimumGitSparseCheckoutVersion)) { + throw new Error( + `Minimum Git version required for sparse checkout is ${minimumGitSparseCheckoutVersion}. Your git ('${this.gitPath}') is ${gitVersion}` + ) + } + } // Set the user agent const gitHttpUserAgent = `git/${gitVersion} (github-actions-checkout)` core.debug(`Set git useragent to: ${gitHttpUserAgent}`) |
