3
0
Fork 0
mirror of https://code.forgejo.org/actions/checkout.git synced 2026-06-23 21:30:33 +00:00
This commit is contained in:
Marcus Wirtz 2026-06-02 14:42:54 -04:00 committed by GitHub
commit 1762f6010a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 50 additions and 29 deletions

View file

@ -44,20 +44,14 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
if (git) {
authHelper = gitAuthHelper.createAuthHelper(git, settings)
if (settings.setSafeDirectory) {
// Setup the repository path as a safe directory, so if we pass this into a container job with a different user it doesn't fail
// Otherwise all git commands we run in a container fail
await authHelper.configureTempGlobalConfig()
core.info(
`Adding repository directory to the temporary git global config as a safe directory`
await addSafeDirectory(settings.repositoryPath, git, authHelper)
const containerPath = getContainerRepositoryPath(
settings.repositoryPath,
settings.githubWorkspacePath
)
await git
.config('safe.directory', settings.repositoryPath, true, true)
.catch(error => {
core.info(
`Failed to initialize safe directory with error: ${error}`
)
})
if (containerPath && containerPath !== settings.repositoryPath) {
await addSafeDirectory(containerPath, git, authHelper)
}
stateHelper.setSafeDirectory()
}
@ -350,18 +344,7 @@ export async function cleanup(repositoryPath: string): Promise<void> {
const authHelper = gitAuthHelper.createAuthHelper(git)
try {
if (stateHelper.PostSetSafeDirectory) {
// Setup the repository path as a safe directory, so if we pass this into a container job with a different user it doesn't fail
// Otherwise all git commands we run in a container fail
await authHelper.configureTempGlobalConfig()
core.info(
`Adding repository directory to the temporary git global config as a safe directory`
)
await git
.config('safe.directory', repositoryPath, true, true)
.catch(error => {
core.info(`Failed to initialize safe directory with error: ${error}`)
})
await addSafeDirectory(repositoryPath, git, authHelper)
}
await authHelper.removeAuth()
@ -390,3 +373,39 @@ async function getGitCommandManager(
return undefined
}
}
async function addSafeDirectory(
safeDirectory: string,
git: IGitCommandManager,
authHelper: gitAuthHelper.IGitAuthHelper
): Promise<void> {
await authHelper.configureTempGlobalConfig()
core.info(`Adding '${safeDirectory}' to the git global config as a safe directory`)
await git.config('safe.directory', safeDirectory, true, true).catch(error => {
core.info(`Failed to initialize safe directory with error: ${error}`)
})
}
function getContainerRepositoryPath(
repositoryPath: string,
githubWorkspace?: string
): string {
if (!githubWorkspace) {
return ''
}
let relativeRepositoryPath = path.relative(githubWorkspace, repositoryPath)
if (!relativeRepositoryPath || relativeRepositoryPath === '.') {
return '/github/workspace'
}
if (
relativeRepositoryPath.startsWith('..') ||
path.isAbsolute(relativeRepositoryPath)
) {
return ''
}
relativeRepositoryPath = relativeRepositoryPath.replace(/\\/g, '/')
return path.posix.join('/github/workspace', relativeRepositoryPath)
}