// Delete any index.lock and shallow.lock left by a previously canceled run or crashed git process
constlockPaths=[
path.join(repositoryPath,'.git','index.lock'),
path.join(repositoryPath,'.git','shallow.lock')
]
for(constlockPathoflockPaths){
try{
awaitio.rmRF(lockPath)
}catch(error){
core.debug(`Unable to delete '${lockPath}'. ${error.message}`)
}
}
try{
// Checkout detached HEAD
if(!(awaitgit.isDetached())){
awaitgit.checkoutDetach()
}
// Remove all refs/heads/*
letbranches=awaitgit.branchList(false)
for(constbranchofbranches){
awaitgit.branchDelete(false,branch)
}
// Remove all refs/remotes/origin/* to avoid conflicts
branches=awaitgit.branchList(true)
for(constbranchofbranches){
awaitgit.branchDelete(true,branch)
}
}catch(error){
core.warning(
`Unable to prepare the existing repository. The repository will be recreated instead.`
)
returnfalse
}
// Clean
if(clean){
letsucceeded=true
if(!(awaitgit.tryClean())){
core.debug(
`The clean command failed. This might be caused by: 1) path too long, 2) permission issue, or 3) file in use. For futher investigation, manually run 'git clean -ffdx' on the directory '${repositoryPath}'.`
)
succeeded=false
}elseif(!(awaitgit.tryReset())){
succeeded=false
}
if(!succeeded){
core.warning(
`Unable to clean or reset the repository. The repository will be recreated instead.`
)
}
returnsucceeded
}
returntrue
}
asyncfunctionremoveGitConfig(
git: IGitCommandManager,
configKey: string
):Promise<void>{
if(
(awaitgit.configExists(configKey))&&
!(awaitgit.tryConfigUnset(configKey))
){
// Load the config contents
core.warning(
`Failed to remove '${configKey}' from the git config. Attempting to remove the config value by editing the file directly.`