mirror of
https://github.com/actions/checkout.git
synced 2025-10-17 00:25:08 +07:00
Split removeGitConfig, improve comments, fix tests, and set GITHUB_WORKSPACE in tests
This commit is contained in:
@ -44,7 +44,7 @@ class GitAuthHelper {
|
||||
private sshKnownHostsPath = ''
|
||||
private temporaryHomePath = ''
|
||||
private credentialsConfigPath = '' // Path to separate credentials config file in RUNNER_TEMP
|
||||
private credentialsIncludeKeys: string[] = [] // Track includeIf/include config keys for cleanup
|
||||
private credentialsIncludeKeys: string[] = [] // Track includeIf config keys for cleanup
|
||||
|
||||
constructor(
|
||||
gitCommandManager: IGitCommandManager,
|
||||
@ -168,24 +168,25 @@ class GitAuthHelper {
|
||||
|
||||
async configureSubmoduleAuth(): Promise<void> {
|
||||
// Remove possible previous HTTPS instead of SSH
|
||||
await this.removeGitConfig(this.insteadOfKey, true)
|
||||
await this.removeSubmoduleGitConfig(this.insteadOfKey)
|
||||
|
||||
if (this.settings.persistCredentials) {
|
||||
// Use the same credentials config file created for the main repo
|
||||
// Credentials config path
|
||||
const credentialsConfigPath = await this.getCredentialsConfigPath()
|
||||
const githubWorkspace = process.env['GITHUB_WORKSPACE']
|
||||
assert.ok(githubWorkspace, 'GITHUB_WORKSPACE is not defined')
|
||||
|
||||
// Container credentials config path
|
||||
const containerCredentialsPath = path.posix.join(
|
||||
'/github/runner_temp',
|
||||
path.basename(credentialsConfigPath)
|
||||
)
|
||||
|
||||
// Calculate container git directory base path
|
||||
// Container repo path
|
||||
const workingDirectory = this.git.getWorkingDirectory()
|
||||
const githubWorkspace = process.env['GITHUB_WORKSPACE']
|
||||
assert.ok(githubWorkspace, 'GITHUB_WORKSPACE is not defined')
|
||||
let relativePath = path.relative(githubWorkspace, workingDirectory)
|
||||
relativePath = relativePath.replace(/\\/g, '/')
|
||||
const containerWorkspaceBase = path.posix.join(
|
||||
const containerRepoPath = path.posix.join(
|
||||
'/github/workspace',
|
||||
relativePath
|
||||
)
|
||||
@ -204,12 +205,12 @@ class GitAuthHelper {
|
||||
// For each submodule, configure includeIf entries pointing to the shared credentials file.
|
||||
// Configure both host and container paths to support Docker container actions.
|
||||
for (const configPath of configPaths) {
|
||||
// Get the submodule path from its config file path.
|
||||
// Submodule path
|
||||
const submodulePath = path.dirname(path.dirname(configPath))
|
||||
// Configure host path includeIf.
|
||||
// Use forward slashes for git config, even on Windows.
|
||||
|
||||
// Configure host includeIf
|
||||
let submoduleGitDir = path.join(submodulePath, '.git')
|
||||
submoduleGitDir = submoduleGitDir.replace(/\\/g, '/')
|
||||
submoduleGitDir = submoduleGitDir.replace(/\\/g, '/') // Use forward slashes, even on Windows
|
||||
await this.git.config(
|
||||
`includeIf.gitdir:${submoduleGitDir}.path`,
|
||||
credentialsConfigPath,
|
||||
@ -218,15 +219,14 @@ class GitAuthHelper {
|
||||
configPath
|
||||
)
|
||||
|
||||
// Configure container path includeIf.
|
||||
// Use forward slashes for git config, even on Windows.
|
||||
// Configure container includeIf
|
||||
let submoduleRelativePath = path.relative(
|
||||
workingDirectory,
|
||||
submodulePath
|
||||
)
|
||||
submoduleRelativePath = submoduleRelativePath.replace(/\\/g, '/')
|
||||
submoduleRelativePath = submoduleRelativePath.replace(/\\/g, '/') // Use forward slashes, even on Windows
|
||||
const containerSubmoduleGitDir = path.posix.join(
|
||||
containerWorkspaceBase,
|
||||
containerRepoPath,
|
||||
submoduleRelativePath,
|
||||
'.git'
|
||||
)
|
||||
@ -365,36 +365,34 @@ class GitAuthHelper {
|
||||
// Global config file is temporary
|
||||
await this.git.config('include.path', credentialsConfigPath, true)
|
||||
} else {
|
||||
// For local config, use includeIf.gitdir to match the .git directory.
|
||||
// Configure for both host and container paths to support Docker container actions.
|
||||
// Host git directory
|
||||
let gitDir = path.join(this.git.getWorkingDirectory(), '.git')
|
||||
// Use forward slashes for git config, even on Windows
|
||||
gitDir = gitDir.replace(/\\/g, '/')
|
||||
gitDir = gitDir.replace(/\\/g, '/') // Use forward slashes, even on Windows
|
||||
|
||||
// Configure host includeIf
|
||||
const hostIncludeKey = `includeIf.gitdir:${gitDir}.path`
|
||||
await this.git.config(hostIncludeKey, credentialsConfigPath)
|
||||
this.credentialsIncludeKeys.push(hostIncludeKey)
|
||||
|
||||
// Configure for container scenario where paths are mapped to fixed locations
|
||||
// Container git directory
|
||||
const githubWorkspace = process.env['GITHUB_WORKSPACE']
|
||||
assert.ok(githubWorkspace, 'GITHUB_WORKSPACE is not defined')
|
||||
|
||||
// Calculate the relative path of the working directory from GITHUB_WORKSPACE
|
||||
const workingDirectory = this.git.getWorkingDirectory()
|
||||
let relativePath = path.relative(githubWorkspace, workingDirectory)
|
||||
|
||||
// Container paths: GITHUB_WORKSPACE -> /github/workspace, RUNNER_TEMP -> /github/runner_temp
|
||||
// Use forward slashes for git config
|
||||
relativePath = relativePath.replace(/\\/g, '/')
|
||||
relativePath = relativePath.replace(/\\/g, '/') // Use forward slashes, even on Windows
|
||||
const containerGitDir = path.posix.join(
|
||||
'/github/workspace',
|
||||
relativePath,
|
||||
'.git'
|
||||
)
|
||||
|
||||
// Container credentials config path
|
||||
const containerCredentialsPath = path.posix.join(
|
||||
'/github/runner_temp',
|
||||
path.basename(credentialsConfigPath)
|
||||
)
|
||||
|
||||
// Configure container includeIf
|
||||
const containerIncludeKey = `includeIf.gitdir:${containerGitDir}.path`
|
||||
await this.git.config(containerIncludeKey, containerCredentialsPath)
|
||||
this.credentialsIncludeKeys.push(containerIncludeKey)
|
||||
@ -444,19 +442,21 @@ class GitAuthHelper {
|
||||
|
||||
// SSH command
|
||||
await this.removeGitConfig(SSH_COMMAND_KEY)
|
||||
await this.removeSubmoduleGitConfig(SSH_COMMAND_KEY)
|
||||
}
|
||||
|
||||
private async removeToken(): Promise<void> {
|
||||
// HTTP extra header
|
||||
// Remove HTTP extra header
|
||||
await this.removeGitConfig(this.tokenConfigKey)
|
||||
await this.removeSubmoduleGitConfig(this.tokenConfigKey)
|
||||
|
||||
// Remove include/includeIf config entries
|
||||
// Remove includeIf
|
||||
for (const includeKey of this.credentialsIncludeKeys) {
|
||||
await this.removeGitConfig(includeKey)
|
||||
}
|
||||
this.credentialsIncludeKeys = []
|
||||
|
||||
// Remove includeIf entries from submodules
|
||||
// Remove submodule includeIf
|
||||
await this.git.submoduleForeach(
|
||||
`sh -c "git config --local --get-regexp '^includeIf\\.' && git config --local --remove-section includeIf || :"`,
|
||||
true
|
||||
@ -475,23 +475,20 @@ class GitAuthHelper {
|
||||
}
|
||||
}
|
||||
|
||||
private async removeGitConfig(
|
||||
configKey: string,
|
||||
submoduleOnly: boolean = false
|
||||
): Promise<void> {
|
||||
if (!submoduleOnly) {
|
||||
if (
|
||||
(await this.git.configExists(configKey)) &&
|
||||
!(await this.git.tryConfigUnset(configKey))
|
||||
) {
|
||||
// Load the config contents
|
||||
core.warning(`Failed to remove '${configKey}' from the git config`)
|
||||
}
|
||||
private async removeGitConfig(configKey: string): Promise<void> {
|
||||
if (
|
||||
(await this.git.configExists(configKey)) &&
|
||||
!(await this.git.tryConfigUnset(configKey))
|
||||
) {
|
||||
// Load the config contents
|
||||
core.warning(`Failed to remove '${configKey}' from the git config`)
|
||||
}
|
||||
}
|
||||
|
||||
private async removeSubmoduleGitConfig(configKey: string): Promise<void> {
|
||||
const pattern = regexpHelper.escape(configKey)
|
||||
await this.git.submoduleForeach(
|
||||
// wrap the pipeline in quotes to make sure it's handled properly by submoduleForeach, rather than just the first part of the pipeline
|
||||
// Wrap the pipeline in quotes to make sure it's handled properly by submoduleForeach, rather than just the first part of the pipeline.
|
||||
`sh -c "git config --local --name-only --get-regexp '${pattern}' && git config --local --unset-all '${configKey}' || :"`,
|
||||
true
|
||||
)
|
||||
|
Reference in New Issue
Block a user