From b0b939aaa52091962fb262a342f6db4c509add69 Mon Sep 17 00:00:00 2001 From: satotake Date: Wed, 20 Oct 2021 23:56:37 +0900 Subject: [PATCH] add `shallow-since` option --- README.md | 4 +++- __test__/git-auth-helper.test.ts | 1 + action.yml | 3 ++- adrs/0153-checkout-v2.md | 4 +++- src/git-command-manager.ts | 29 +++++++++++++++++++++++++---- src/git-source-provider.ts | 5 +++-- src/git-source-settings.ts | 5 +++++ src/input-helper.ts | 10 ++++++++++ 8 files changed, 52 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9c56a6f..d2c137d 100644 --- a/README.md +++ b/README.md @@ -90,9 +90,11 @@ Refer [here](https://github.com/actions/checkout/blob/v1/README.md) for previous clean: '' # Number of commits to fetch. 0 indicates all history for all branches and tags. - # Default: 1 fetch-depth: '' + # Date like `2days` or `1970-01-01`. Fetch a history after the specified time. + shallow-since: '' + # Whether to download Git-LFS files # Default: false lfs: '' diff --git a/__test__/git-auth-helper.test.ts b/__test__/git-auth-helper.test.ts index b39d352..352db97 100644 --- a/__test__/git-auth-helper.test.ts +++ b/__test__/git-auth-helper.test.ts @@ -760,6 +760,7 @@ async function setup(testName: string): Promise { clean: true, commit: '', fetchDepth: 1, + shallowSince: '', lfs: false, submodules: false, nestedSubmodules: false, diff --git a/action.yml b/action.yml index 91d3982..c36189d 100644 --- a/action.yml +++ b/action.yml @@ -55,7 +55,8 @@ inputs: default: true fetch-depth: description: 'Number of commits to fetch. 0 indicates all history for all branches and tags.' - default: 1 + shallow-since: + description: 'Date like `2days` or `1970-01-01`. Fetch a history after the specified time.' lfs: description: 'Whether to download Git-LFS files' default: false diff --git a/adrs/0153-checkout-v2.md b/adrs/0153-checkout-v2.md index 74730c7..880f608 100644 --- a/adrs/0153-checkout-v2.md +++ b/adrs/0153-checkout-v2.md @@ -72,6 +72,8 @@ We want to take this opportunity to make behavioral changes, from v1. This docum fetch-depth: description: 'Number of commits to fetch. 0 indicates all history for all tags and branches.' default: 1 + shallow-since: + description: 'Date like `2days` or `1970-01-01`. Fetch a history after the specified time.' lfs: description: 'Whether to download Git-LFS files' default: false @@ -155,7 +157,7 @@ Fetch only the SHA being built and set depth=1. This significantly reduces the f If a SHA isn't available (e.g. multi repo), then fetch only the specified ref with depth=1. -The input `fetch-depth` can be used to control the depth. +The input `fetch-depth` and `shallow-since` can be used to control the depth. Note: - Fetching a single commit is supported by Git wire protocol version 2. The git client uses protocol version 0 by default. The desired protocol version can be overridden in the git config or on the fetch command line invocation (`-c protocol.version=2`). We will override on the fetch command line, for transparency. diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index 409a161..6e3b4d2 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -24,7 +24,11 @@ export interface IGitCommandManager { globalConfig?: boolean ): Promise configExists(configKey: string, globalConfig?: boolean): Promise - fetch(refSpec: string[], fetchDepth?: number): Promise + fetch( + refSpec: string[], + fetchDepth?: number, + shallowSince?: string + ): Promise getDefaultBranch(repositoryUrl: string): Promise getWorkingDirectory(): string init(): Promise @@ -39,7 +43,11 @@ export interface IGitCommandManager { shaExists(sha: string): Promise submoduleForeach(command: string, recursive: boolean): Promise submoduleSync(recursive: boolean): Promise - submoduleUpdate(fetchDepth: number, recursive: boolean): Promise + submoduleUpdate( + fetchDepth: number, + recursive: boolean, + shallowSince?: string + ): Promise tagExists(pattern: string): Promise tryClean(): Promise tryConfigUnset(configKey: string, globalConfig?: boolean): Promise @@ -168,7 +176,11 @@ class GitCommandManager { return output.exitCode === 0 } - async fetch(refSpec: string[], fetchDepth?: number): Promise { + async fetch( + refSpec: string[], + fetchDepth?: number, + shallowSince?: string + ): Promise { const args = ['-c', 'protocol.version=2', 'fetch'] if (!refSpec.some(x => x === refHelper.tagsRefSpec)) { args.push('--no-tags') @@ -177,6 +189,8 @@ class GitCommandManager { args.push('--prune', '--progress', '--no-recurse-submodules') if (fetchDepth && fetchDepth > 0) { args.push(`--depth=${fetchDepth}`) + } else if (shallowSince) { + args.push(`--shallow-since=${shallowSince}`) } else if ( fshelper.fileExistsSync( path.join(this.workingDirectory, '.git', 'shallow') @@ -310,12 +324,19 @@ class GitCommandManager { await this.execGit(args) } - async submoduleUpdate(fetchDepth: number, recursive: boolean): Promise { + async submoduleUpdate( + fetchDepth: number, + recursive: boolean, + shallowSince?: string + ): Promise { const args = ['-c', 'protocol.version=2'] args.push('submodule', 'update', '--init', '--force') if (fetchDepth > 0) { args.push(`--depth=${fetchDepth}`) } + if (shallowSince) { + args.push(`--shallow-since=${shallowSince}`) + } if (recursive) { args.push('--recursive') diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts index 42a12e0..b307f80 100644 --- a/src/git-source-provider.ts +++ b/src/git-source-provider.ts @@ -141,7 +141,7 @@ export async function getSource(settings: IGitSourceSettings): Promise { } } else { const refSpec = refHelper.getRefSpec(settings.ref, settings.commit) - await git.fetch(refSpec, settings.fetchDepth) + await git.fetch(refSpec, settings.fetchDepth, settings.shallowSince) } core.endGroup() @@ -181,7 +181,8 @@ export async function getSource(settings: IGitSourceSettings): Promise { await git.submoduleSync(settings.nestedSubmodules) await git.submoduleUpdate( settings.fetchDepth, - settings.nestedSubmodules + settings.nestedSubmodules, + settings.shallowSince ) await git.submoduleForeach( 'git config --local gc.auto 0', diff --git a/src/git-source-settings.ts b/src/git-source-settings.ts index 2786222..6c7f5f6 100644 --- a/src/git-source-settings.ts +++ b/src/git-source-settings.ts @@ -34,6 +34,11 @@ export interface IGitSourceSettings { */ fetchDepth: number + /** + * The date which a history after is fetched + */ + shallowSince: string + /** * Indicates whether to fetch LFS objects */ diff --git a/src/input-helper.ts b/src/input-helper.ts index 4c05d6e..b09e59c 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -88,6 +88,16 @@ export function getInputs(): IGitSourceSettings { } core.debug(`fetch depth = ${result.fetchDepth}`) + // Shallow since + result.shallowSince = core.getInput('shallow-since') + core.debug(`shallow since = ${result.shallowSince}`) + + if (result.fetchDepth > 0 && result.shallowSince) { + throw new Error( + '`fetch-depath` and `shallow-since` cannot be used at the same time' + ) + } + // LFS result.lfs = (core.getInput('lfs') || 'false').toUpperCase() === 'TRUE' core.debug(`lfs = ${result.lfs}`)