try preserve local changes option

This commit is contained in:
Salman Muin Kayser Chishti 2025-08-11 12:21:31 +01:00
parent 9f265659d3
commit 45fe6460ed
6 changed files with 59 additions and 6 deletions

View File

@ -96,6 +96,11 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
# Default: true
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.
# Default: null
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
## 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
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:

View File

@ -56,7 +56,10 @@ inputs:
description: 'Relative path under $GITHUB_WORKSPACE to place the repository'
clean:
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:
description: >
Partially clone against a given filter.

View File

@ -22,7 +22,7 @@ export interface IGitCommandManager {
disableSparseCheckout(): Promise<void>
sparseCheckout(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>
config(
configKey: string,
@ -203,8 +203,21 @@ class GitCommandManager {
)
}
async checkout(ref: string, startPoint: string): Promise<void> {
const args = ['checkout', '--progress', '--force']
async checkout(
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) {
args.push('-B', ref, startPoint)
} else {

View File

@ -229,7 +229,15 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
// Checkout
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()
// Submodules

View File

@ -25,10 +25,15 @@ export interface IGitSourceSettings {
commit: string
/**
* Indicates whether to clean the repository
* Whether to execute git clean and git reset before fetching
*/
clean: boolean
/**
* Whether to preserve local changes during checkout
*/
preserveLocalChanges: boolean
/**
* The filter determining which objects to include
*/

View File

@ -82,6 +82,10 @@ export async function getInputs(): Promise<IGitSourceSettings> {
result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'
core.debug(`clean = ${result.clean}`)
// Preserve local changes
result.preserveLocalChanges = (core.getInput('preserveLocalChanges') || 'false').toUpperCase() === 'TRUE'
core.debug(`preserveLocalChanges = ${result.preserveLocalChanges}`)
// Filter
const filter = core.getInput('filter')
if (filter) {