Skip to content

Add --shallow-since option #2221

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
# Default: 1
fetch-depth: ''

# Date like `2days` or `1970-01-01`. Fetch a history after the specified time.
shallow-since: ''

# Whether to fetch tags, even if fetch-depth > 0.
# Default: false
fetch-tags: ''
Expand Down
1 change: 1 addition & 0 deletions __test__/git-auth-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ async function setup(testName: string): Promise<void> {
sparseCheckout: [],
sparseCheckoutConeMode: true,
fetchDepth: 1,
shallowSince: '',
fetchTags: false,
showProgress: true,
lfs: false,
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ inputs:
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.'
fetch-tags:
description: 'Whether to fetch tags, even if fetch-depth > 0.'
default: false
Expand Down
4 changes: 3 additions & 1 deletion adrs/0153-checkout-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 13 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,9 @@ class GitCommandManager {
if (options.filter) {
args.push(`--filter=${options.filter}`);
}
if (options.shallowSince) {
args.push(`--shallow-since=${options.shallowSince}`);
}
if (options.fetchDepth && options.fetchDepth > 0) {
args.push(`--depth=${options.fetchDepth}`);
}
Expand Down Expand Up @@ -793,13 +796,16 @@ class GitCommandManager {
yield this.execGit(args);
});
}
submoduleUpdate(fetchDepth, recursive) {
submoduleUpdate(fetchDepth, recursive, shallowSince) {
return __awaiter(this, void 0, void 0, function* () {
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');
}
Expand Down Expand Up @@ -1787,6 +1793,12 @@ function getInputs() {
result.fetchDepth = 0;
}
core.debug(`fetch depth = ${result.fetchDepth}`);
// Shallow since
if (core.getInput('fetch-depth') && core.getInput('shallow-since')) {
throw new Error('`fetch-depth` and `shallow-since` cannot be used at the same time');
}
result.shallowSince = core.getInput('shallow-since');
core.debug(`shallow since = ${result.shallowSince}`);
// Fetch tags
result.fetchTags =
(core.getInput('fetch-tags') || 'false').toUpperCase() === 'TRUE';
Expand Down
22 changes: 20 additions & 2 deletions src/git-command-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface IGitCommandManager {
options: {
filter?: string
fetchDepth?: number
shallowSince?: string
fetchTags?: boolean
showProgress?: boolean
}
Expand All @@ -54,8 +55,12 @@ export interface IGitCommandManager {
shaExists(sha: string): Promise<boolean>
submoduleForeach(command: string, recursive: boolean): Promise<string>
submoduleSync(recursive: boolean): Promise<void>
submoduleUpdate(fetchDepth: number, recursive: boolean): Promise<void>
submoduleStatus(): Promise<boolean>
submoduleUpdate(
fetchDepth: number,
recursive: boolean,
shallowSince?: string
): Promise<void>
tagExists(pattern: string): Promise<boolean>
tryClean(): Promise<boolean>
tryConfigUnset(configKey: string, globalConfig?: boolean): Promise<boolean>
Expand Down Expand Up @@ -256,6 +261,7 @@ class GitCommandManager {
options: {
filter?: string
fetchDepth?: number
shallowSince?: string
fetchTags?: boolean
showProgress?: boolean
}
Expand All @@ -274,6 +280,10 @@ class GitCommandManager {
args.push(`--filter=${options.filter}`)
}

if (options.shallowSince) {
args.push(`--shallow-since=${options.shallowSince}`)
}

if (options.fetchDepth && options.fetchDepth > 0) {
args.push(`--depth=${options.fetchDepth}`)
} else if (
Expand Down Expand Up @@ -409,13 +419,21 @@ class GitCommandManager {
await this.execGit(args)
}

async submoduleUpdate(fetchDepth: number, recursive: boolean): Promise<void> {
async submoduleUpdate(
fetchDepth: number,
recursive: boolean,
shallowSince?: string
): Promise<void> {
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')
}
Expand Down
7 changes: 6 additions & 1 deletion src/git-source-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ export interface IGitSourceSettings {
fetchDepth: number

/**
* Fetch tags, even if fetchDepth > 0 (default: false)
* Deepen or shorten the history of a shallow repository to include all reachable commits after
*/
shallowSince: string

/**
* Fetch tags, even if fetchDepth > 0 (default: false)
*/
fetchTags: boolean

Expand Down
9 changes: 9 additions & 0 deletions src/input-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ export async function getInputs(): Promise<IGitSourceSettings> {
}
core.debug(`fetch depth = ${result.fetchDepth}`)

// Shallow since
if (core.getInput('fetch-depth') && core.getInput('shallow-since')) {
throw new Error(
'`fetch-depth` and `shallow-since` cannot be used at the same time'
)
}
result.shallowSince = core.getInput('shallow-since')
core.debug(`shallow since = ${result.shallowSince}`)

// Fetch tags
result.fetchTags =
(core.getInput('fetch-tags') || 'false').toUpperCase() === 'TRUE'
Expand Down