Skip to content
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

Default branch checkout option #1615

Open
wants to merge 4 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
# Otherwise, uses the default branch.
ref: ''

# Indicates whether to checkout the default repository branch if the requested ref
# does not exist
# Default: false
default-branch-checkout: ''

# Personal access token (PAT) used to fetch the repository. The PAT is configured
# with the local git config, which enables your scripts to run authenticated git
# commands. The post-job step removes the PAT.
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 @@ -813,6 +813,7 @@ async function setup(testName: string): Promise<void> {
nestedSubmodules: false,
persistCredentials: true,
ref: 'refs/heads/main',
defaultBranchCheckout: false,
repositoryName: 'my-repo',
repositoryOwner: 'my-org',
repositoryPath: '',
Expand Down
1 change: 1 addition & 0 deletions __test__/input-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('input-helper tests', () => {
expect(settings.commit).toBeTruthy()
expect(settings.commit).toBe('1234567890123456789012345678901234567890')
expect(settings.filter).toBe(undefined)
expect(settings.defaultBranchCheckout).toBe(false)
expect(settings.sparseCheckout).toBe(undefined)
expect(settings.sparseCheckoutConeMode).toBe(true)
expect(settings.fetchDepth).toBe(1)
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ inputs:
The branch, tag or SHA to checkout. When checking out the repository that
triggered a workflow, this defaults to the reference or SHA for that
event. Otherwise, uses the default branch.
default-branch-checkout:
description: 'Indicates whether to checkout the default repository branch if the requested ref does not exist'
default: false
token:
description: >
Personal access token (PAT) used to fetch the repository. The PAT is configured
Expand Down
24 changes: 22 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ function getSource(settings) {
else if (settings.sparseCheckout) {
fetchOptions.filter = 'blob:none';
}
if (settings.fetchDepth <= 0) {
if (settings.fetchDepth <= 0 || settings.defaultBranchCheckout) {
// Fetch all branches and tags
let refSpec = refHelper.getRefSpecForAllHistory(settings.ref, settings.commit);
yield git.fetch(refSpec, fetchOptions);
Expand All @@ -1270,7 +1270,22 @@ function getSource(settings) {
core.endGroup();
// Checkout info
core.startGroup('Determining the checkout info');
const checkoutInfo = yield refHelper.getCheckoutInfo(git, settings.ref, settings.commit);
let checkoutInfo;
try {
checkoutInfo = yield refHelper.getCheckoutInfo(git, settings.ref, settings.commit);
}
catch (error) {
if (settings.defaultBranchCheckout) {
core.info('Could not determine the checkout info. Trying the default repository branch');
const repositoryDefaultBranch = settings.sshKey
? yield git.getDefaultBranch(repositoryUrl)
: yield githubApiHelper.getDefaultBranch(settings.authToken, settings.repositoryOwner, settings.repositoryName);
checkoutInfo = yield refHelper.getCheckoutInfo(git, repositoryDefaultBranch, settings.commit);
}
else {
throw error;
}
}
core.endGroup();
// LFS fetch
// Explicit lfs-fetch to avoid slow checkout (fetches one lfs object at a time).
Expand Down Expand Up @@ -1724,6 +1739,11 @@ function getInputs() {
}
core.debug(`ref = '${result.ref}'`);
core.debug(`commit = '${result.commit}'`);
// Default branch checkout
result.defaultBranchCheckout =
(core.getInput('default-branch-checkout') || 'false').toUpperCase() ===
'TRUE';
core.debug(`default-branch-checkout = '${result.defaultBranchCheckout}'`);
// Clean
result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE';
core.debug(`clean = ${result.clean}`);
Expand Down
35 changes: 29 additions & 6 deletions src/git-source-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
fetchOptions.filter = 'blob:none'
}

if (settings.fetchDepth <= 0) {
if (settings.fetchDepth <= 0 || settings.defaultBranchCheckout) {
// Fetch all branches and tags
let refSpec = refHelper.getRefSpecForAllHistory(
settings.ref,
Expand All @@ -190,11 +190,34 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {

// Checkout info
core.startGroup('Determining the checkout info')
const checkoutInfo = await refHelper.getCheckoutInfo(
git,
settings.ref,
settings.commit
)
let checkoutInfo: refHelper.ICheckoutInfo
try {
checkoutInfo = await refHelper.getCheckoutInfo(
git,
settings.ref,
settings.commit
)
} catch (error) {
if (settings.defaultBranchCheckout) {
core.info(
'Could not determine the checkout info. Trying the default repository branch'
)
const repositoryDefaultBranch = settings.sshKey
? await git.getDefaultBranch(repositoryUrl)
: await githubApiHelper.getDefaultBranch(
settings.authToken,
settings.repositoryOwner,
settings.repositoryName
)
checkoutInfo = await refHelper.getCheckoutInfo(
git,
repositoryDefaultBranch,
settings.commit
)
} else {
throw error
}
}
core.endGroup()

// LFS fetch
Expand Down
5 changes: 5 additions & 0 deletions src/git-source-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export interface IGitSourceSettings {
*/
ref: string

/**
* Indicates whether to checkout the default repository branch if the requested ref does not exist
*/
defaultBranchCheckout: boolean

/**
* The commit to checkout
*/
Expand Down
6 changes: 6 additions & 0 deletions src/input-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ export async function getInputs(): Promise<IGitSourceSettings> {
core.debug(`ref = '${result.ref}'`)
core.debug(`commit = '${result.commit}'`)

// Default branch checkout
result.defaultBranchCheckout =
(core.getInput('default-branch-checkout') || 'false').toUpperCase() ===
'TRUE'
core.debug(`default-branch-checkout = '${result.defaultBranchCheckout}'`)

// Clean
result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'
core.debug(`clean = ${result.clean}`)
Expand Down