This commit is contained in:
Salman Muin Kayser Chishti 2025-08-11 12:47:29 +01:00
parent 6503dcd44c
commit 630cdb3874
4 changed files with 36 additions and 22 deletions

View File

@ -209,7 +209,7 @@ class GitCommandManager {
options: string[] = []
): Promise<void> {
const args = ['checkout', '--progress']
// Add custom options (like --merge) if provided
if (options.length > 0) {
args.push(...options)
@ -217,7 +217,7 @@ class GitCommandManager {
// Default behavior - use force
args.push('--force')
}
if (startPoint) {
args.push('-B', ref, startPoint)
} else {

View File

@ -19,10 +19,12 @@ export async function prepareExistingDirectory(
// 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`)
core.info(
`Preserve local changes is enabled, will attempt to keep local files`
)
}
// Check whether using git or REST API
@ -132,11 +134,17 @@ export async function prepareExistingDirectory(
await io.rmRF(path.join(repositoryPath, file))
}
} else if (remove && preserveLocalChanges) {
core.info(`Skipping deletion of directory contents due to preserve-local-changes setting`)
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 })
core.info(
`Initializing git repository to prepare for checkout with preserved changes`
)
await fs.promises.mkdir(path.join(repositoryPath, '.git'), {
recursive: true
})
}
}
}

View File

@ -232,17 +232,17 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
core.startGroup('Checking out the ref')
if (settings.preserveLocalChanges) {
core.info('Attempting to preserve local changes during checkout')
// List and store local files before checkout
const fs = require('fs')
const path = require('path')
const localFiles = new Map()
try {
// Get all files in the workspace that aren't in the .git directory
const workspacePath = process.cwd()
core.info(`Current workspace path: ${workspacePath}`)
// List all files in the current directory using fs
const listFilesRecursively = (dir: string): string[] => {
let results: string[] = []
@ -252,7 +252,7 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
const relativePath = path.relative(workspacePath, fullPath)
// Skip .git directory
if (relativePath.startsWith('.git')) return
const stat = fs.statSync(fullPath)
if (stat && stat.isDirectory()) {
// Recursively explore subdirectories
@ -270,17 +270,17 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
})
return results
}
const localFilesList = listFilesRecursively(workspacePath)
core.info(`Found ${localFilesList.length} local files to preserve:`)
localFilesList.forEach(file => core.info(` - ${file}`))
} catch (error) {
core.warning(`Failed to list local files: ${error}`)
}
// Perform normal checkout
await git.checkout(checkoutInfo.ref, checkoutInfo.startPoint)
// Restore local files that were not tracked by git
core.info('Restoring local files after checkout')
try {
@ -290,16 +290,16 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
silent: true,
ignoreReturnCode: true
}
for (const [filePath, content] of localFiles.entries()) {
// Check if file exists in git using a child process instead of git.execGit
const { exec } = require('@actions/exec')
const {exec} = require('@actions/exec')
let exitCode = 0
const output = {
stdout: '',
stderr: ''
}
// Capture output
const options = {
...execOptions,
@ -312,14 +312,18 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
}
}
}
exitCode = await exec('git', ['ls-files', '--error-unmatch', filePath], options)
exitCode = await exec(
'git',
['ls-files', '--error-unmatch', filePath],
options
)
if (exitCode !== 0) {
// File is not tracked by git, safe to restore
const fullPath = path.join(process.cwd(), filePath)
// Ensure directory exists
fs.mkdirSync(path.dirname(fullPath), { recursive: true })
fs.mkdirSync(path.dirname(fullPath), {recursive: true})
fs.writeFileSync(fullPath, content)
core.info(`Restored local file: ${filePath}`)
restoredCount++

View File

@ -83,7 +83,9 @@ export async function getInputs(): Promise<IGitSourceSettings> {
core.debug(`clean = ${result.clean}`)
// Preserve local changes
result.preserveLocalChanges = (core.getInput('preserve-local-changes') || 'false').toUpperCase() === 'TRUE'
result.preserveLocalChanges =
(core.getInput('preserve-local-changes') || 'false').toUpperCase() ===
'TRUE'
core.debug(`preserveLocalChanges = ${result.preserveLocalChanges}`)
// Filter