1
0
Fork 0
mirror of https://code.forgejo.org/actions/checkout.git synced 2025-08-14 17:30:50 +00:00
This commit is contained in:
Salman Muin Kayser Chishti 2025-08-11 12:39:11 +01:00
parent ebd82bae91
commit 6503dcd44c
3 changed files with 41 additions and 7 deletions

View file

@ -11,13 +11,19 @@ export async function prepareExistingDirectory(
repositoryPath: string,
repositoryUrl: string,
clean: boolean,
ref: string
ref: string,
preserveLocalChanges: boolean = false
): Promise<void> {
assert.ok(repositoryPath, 'Expected repositoryPath to be defined')
assert.ok(repositoryUrl, 'Expected repositoryUrl to be defined')
// Indicates whether to delete the directory contents
let remove = false
// If preserveLocalChanges is true, log it
if (preserveLocalChanges) {
core.info(`Preserve local changes is enabled, will attempt to keep local files`)
}
// Check whether using git or REST API
if (!git) {
@ -114,12 +120,23 @@ export async function prepareExistingDirectory(
}
}
if (remove) {
if (remove && !preserveLocalChanges) {
// Delete the contents of the directory. Don't delete the directory itself
// since it might be the current working directory.
core.info(`Deleting the contents of '${repositoryPath}'`)
for (const file of await fs.promises.readdir(repositoryPath)) {
// Skip .git directory as we need it to determine if a file is tracked
if (file === '.git') {
continue
}
await io.rmRF(path.join(repositoryPath, file))
}
} else if (remove && preserveLocalChanges) {
core.info(`Skipping deletion of directory contents due to preserve-local-changes setting`)
// We still need to make sure we have a git repository to work with
if (!git) {
core.info(`Initializing git repository to prepare for checkout with preserved changes`)
await fs.promises.mkdir(path.join(repositoryPath, '.git'), { recursive: true })
}
}
}

View file

@ -70,7 +70,8 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
settings.repositoryPath,
repositoryUrl,
settings.clean,
settings.ref
settings.ref,
settings.preserveLocalChanges
)
}