diff --git a/README.md b/README.md index 1e94e04..6f9e8bb 100644 --- a/README.md +++ b/README.md @@ -97,9 +97,10 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/ 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. + # local files that are not tracked by Git. By default, all files will be + # overwritten. # Default: false - preserveLocalChanges: '' + preserve-local-changes: '' # Partially clone against a given filter. Overrides sparse-checkout if set. # Default: null @@ -349,7 +350,7 @@ steps: uses: actions/checkout@v5 with: clean: false - preserveLocalChanges: true + preserve-local-changes: true ``` # Recommended permissions diff --git a/action.yml b/action.yml index 26d5a5b..0665486 100644 --- a/action.yml +++ b/action.yml @@ -57,7 +57,7 @@ inputs: clean: description: 'Whether to execute `git clean -ffdx && git reset --hard HEAD` before fetching' default: 'true' - preserveLocalChanges: + preserve-local-changes: 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: diff --git a/dist/index.js b/dist/index.js index f3ae6f3..e91b36b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -609,9 +609,17 @@ class GitCommandManager { yield fs.promises.appendFile(sparseCheckoutPath, `\n${sparseCheckout.join('\n')}\n`); }); } - checkout(ref, startPoint) { - return __awaiter(this, void 0, void 0, function* () { - const args = ['checkout', '--progress', '--force']; + checkout(ref_1, startPoint_1) { + return __awaiter(this, arguments, void 0, function* (ref, startPoint, options = []) { + 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); } @@ -1329,7 +1337,16 @@ function getSource(settings) { } // Checkout core.startGroup('Checking out the ref'); - yield 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 + yield git.checkout(checkoutInfo.ref, checkoutInfo.startPoint, ['--merge']); + } + else { + // Use the default behavior with --force + yield git.checkout(checkoutInfo.ref, checkoutInfo.startPoint); + } core.endGroup(); // Submodules if (settings.submodules) { @@ -1766,6 +1783,9 @@ function getInputs() { // Clean result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'; core.debug(`clean = ${result.clean}`); + // Preserve local changes + result.preserveLocalChanges = (core.getInput('preserve-local-changes') || 'false').toUpperCase() === 'TRUE'; + core.debug(`preserveLocalChanges = ${result.preserveLocalChanges}`); // Filter const filter = core.getInput('filter'); if (filter) { diff --git a/src/input-helper.ts b/src/input-helper.ts index 833cdcd..49eafc7 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -83,7 +83,7 @@ export async function getInputs(): Promise { core.debug(`clean = ${result.clean}`) // Preserve local changes - result.preserveLocalChanges = (core.getInput('preserveLocalChanges') || 'false').toUpperCase() === 'TRUE' + result.preserveLocalChanges = (core.getInput('preserve-local-changes') || 'false').toUpperCase() === 'TRUE' core.debug(`preserveLocalChanges = ${result.preserveLocalChanges}`) // Filter