mirror of
https://github.com/actions/checkout.git
synced 2025-08-11 17:51:01 +00:00
try preserve local changes option
This commit is contained in:
parent
9f265659d3
commit
45fe6460ed
6 changed files with 59 additions and 6 deletions
20
README.md
20
README.md
|
@ -96,6 +96,11 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
|
||||||
# Default: true
|
# Default: true
|
||||||
clean: ''
|
clean: ''
|
||||||
|
|
||||||
|
# Whether to preserve local changes during checkout. If true, tries to preserve
|
||||||
|
# local files that are not tracked by Git. By default, all files will be overwritten.
|
||||||
|
# Default: false
|
||||||
|
preserveLocalChanges: ''
|
||||||
|
|
||||||
# Partially clone against a given filter. Overrides sparse-checkout if set.
|
# Partially clone against a given filter. Overrides sparse-checkout if set.
|
||||||
# Default: null
|
# Default: null
|
||||||
filter: ''
|
filter: ''
|
||||||
|
@ -332,6 +337,21 @@ jobs:
|
||||||
|
|
||||||
*NOTE:* The user email is `{user.id}+{user.login}@users.noreply.github.com`. See users API: https://api.github.com/users/github-actions%5Bbot%5D
|
*NOTE:* The user email is `{user.id}+{user.login}@users.noreply.github.com`. See users API: https://api.github.com/users/github-actions%5Bbot%5D
|
||||||
|
|
||||||
|
## Preserve local changes during checkout
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
steps:
|
||||||
|
- name: Create file before checkout
|
||||||
|
shell: pwsh
|
||||||
|
run: New-Item -Path . -Name "example.txt" -ItemType "File"
|
||||||
|
|
||||||
|
- name: Checkout with preserving local changes
|
||||||
|
uses: actions/checkout@v5
|
||||||
|
with:
|
||||||
|
clean: false
|
||||||
|
preserveLocalChanges: true
|
||||||
|
```
|
||||||
|
|
||||||
# Recommended permissions
|
# Recommended permissions
|
||||||
|
|
||||||
When using the `checkout` action in your GitHub Actions workflow, it is recommended to set the following `GITHUB_TOKEN` permissions to ensure proper functionality, unless alternative auth is provided via the `token` or `ssh-key` inputs:
|
When using the `checkout` action in your GitHub Actions workflow, it is recommended to set the following `GITHUB_TOKEN` permissions to ensure proper functionality, unless alternative auth is provided via the `token` or `ssh-key` inputs:
|
||||||
|
|
|
@ -56,7 +56,10 @@ inputs:
|
||||||
description: 'Relative path under $GITHUB_WORKSPACE to place the repository'
|
description: 'Relative path under $GITHUB_WORKSPACE to place the repository'
|
||||||
clean:
|
clean:
|
||||||
description: 'Whether to execute `git clean -ffdx && git reset --hard HEAD` before fetching'
|
description: 'Whether to execute `git clean -ffdx && git reset --hard HEAD` before fetching'
|
||||||
default: true
|
default: 'true'
|
||||||
|
preserveLocalChanges:
|
||||||
|
description: 'Whether to preserve local changes during checkout. If true, tries to preserve local files that are not tracked by Git. By default, all files will be overwritten.'
|
||||||
|
default: 'false'
|
||||||
filter:
|
filter:
|
||||||
description: >
|
description: >
|
||||||
Partially clone against a given filter.
|
Partially clone against a given filter.
|
||||||
|
|
|
@ -22,7 +22,7 @@ export interface IGitCommandManager {
|
||||||
disableSparseCheckout(): Promise<void>
|
disableSparseCheckout(): Promise<void>
|
||||||
sparseCheckout(sparseCheckout: string[]): Promise<void>
|
sparseCheckout(sparseCheckout: string[]): Promise<void>
|
||||||
sparseCheckoutNonConeMode(sparseCheckout: string[]): Promise<void>
|
sparseCheckoutNonConeMode(sparseCheckout: string[]): Promise<void>
|
||||||
checkout(ref: string, startPoint: string): Promise<void>
|
checkout(ref: string, startPoint: string, options?: string[]): Promise<void>
|
||||||
checkoutDetach(): Promise<void>
|
checkoutDetach(): Promise<void>
|
||||||
config(
|
config(
|
||||||
configKey: string,
|
configKey: string,
|
||||||
|
@ -203,8 +203,21 @@ class GitCommandManager {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
async checkout(ref: string, startPoint: string): Promise<void> {
|
async checkout(
|
||||||
const args = ['checkout', '--progress', '--force']
|
ref: string,
|
||||||
|
startPoint: string,
|
||||||
|
options: string[] = []
|
||||||
|
): Promise<void> {
|
||||||
|
const args = ['checkout', '--progress']
|
||||||
|
|
||||||
|
// Add custom options (like --merge) if provided
|
||||||
|
if (options.length > 0) {
|
||||||
|
args.push(...options)
|
||||||
|
} else {
|
||||||
|
// Default behavior - use force
|
||||||
|
args.push('--force')
|
||||||
|
}
|
||||||
|
|
||||||
if (startPoint) {
|
if (startPoint) {
|
||||||
args.push('-B', ref, startPoint)
|
args.push('-B', ref, startPoint)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -229,7 +229,15 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
|
||||||
|
|
||||||
// Checkout
|
// Checkout
|
||||||
core.startGroup('Checking out the ref')
|
core.startGroup('Checking out the ref')
|
||||||
await git.checkout(checkoutInfo.ref, checkoutInfo.startPoint)
|
if (settings.preserveLocalChanges) {
|
||||||
|
core.info('Attempting to preserve local changes during checkout')
|
||||||
|
// Use --merge to preserve local changes if possible
|
||||||
|
// This will fail if there are merge conflicts, but that's expected behavior
|
||||||
|
await git.checkout(checkoutInfo.ref, checkoutInfo.startPoint, ['--merge'])
|
||||||
|
} else {
|
||||||
|
// Use the default behavior with --force
|
||||||
|
await git.checkout(checkoutInfo.ref, checkoutInfo.startPoint)
|
||||||
|
}
|
||||||
core.endGroup()
|
core.endGroup()
|
||||||
|
|
||||||
// Submodules
|
// Submodules
|
||||||
|
|
|
@ -25,10 +25,15 @@ export interface IGitSourceSettings {
|
||||||
commit: string
|
commit: string
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates whether to clean the repository
|
* Whether to execute git clean and git reset before fetching
|
||||||
*/
|
*/
|
||||||
clean: boolean
|
clean: boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to preserve local changes during checkout
|
||||||
|
*/
|
||||||
|
preserveLocalChanges: boolean
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The filter determining which objects to include
|
* The filter determining which objects to include
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -82,6 +82,10 @@ export async function getInputs(): Promise<IGitSourceSettings> {
|
||||||
result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'
|
result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'
|
||||||
core.debug(`clean = ${result.clean}`)
|
core.debug(`clean = ${result.clean}`)
|
||||||
|
|
||||||
|
// Preserve local changes
|
||||||
|
result.preserveLocalChanges = (core.getInput('preserveLocalChanges') || 'false').toUpperCase() === 'TRUE'
|
||||||
|
core.debug(`preserveLocalChanges = ${result.preserveLocalChanges}`)
|
||||||
|
|
||||||
// Filter
|
// Filter
|
||||||
const filter = core.getInput('filter')
|
const filter = core.getInput('filter')
|
||||||
if (filter) {
|
if (filter) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue