Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ jobs:
fi
# Verify auth token
git config --global --add safe.directory "*"
git fetch --no-tags --depth=1 origin +refs/heads/main:refs/remotes/origin/main
# needed to make checkout post cleanup succeed
Expand Down
3 changes: 0 additions & 3 deletions __test__/verify-worktree.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ fi

cd "$CHECKOUT_PATH"

# Add safe directory for container environments
git config --global --add safe.directory "*" 2>/dev/null || true

# Show the includeIf configuration
echo "Git config includeIf entries:"
git config --list --show-origin | grep -i include || true
Expand Down
54 changes: 41 additions & 13 deletions src/git-source-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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)
}
Comment thread
marcus-wirtz-snkeos marked this conversation as resolved.

stateHelper.setSafeDirectory()
}
Expand Down Expand Up @@ -373,3 +367,37 @@ async function getGitCommandManager(
return undefined
}
}

async function addSafeDirectory(
safeDirectory: string,
git: IGitCommandManager
): Promise<void> {
core.info(`Adding '${safeDirectory}' to the git global config as a safe directory`)
Comment thread
marcus-wirtz-snkeos marked this conversation as resolved.
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)
}
5 changes: 5 additions & 0 deletions src/git-source-settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export interface IGitSourceSettings {
/**
* The workflow workspace path
*/
githubWorkspacePath?: string

/**
* The location on disk where the repository will be placed
*/
Expand Down
1 change: 1 addition & 0 deletions src/input-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export async function getInputs(): Promise<IGitSourceSettings> {
throw new Error('GITHUB_WORKSPACE not defined')
}
githubWorkspacePath = path.resolve(githubWorkspacePath)
result.githubWorkspacePath = githubWorkspacePath
core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`)
fsHelper.directoryExistsSync(githubWorkspacePath, true)

Expand Down
Loading