diff --git a/src/git.ts b/src/git.ts index 3a4cf1cb..8f689d4e 100644 --- a/src/git.ts +++ b/src/git.ts @@ -21,53 +21,52 @@ export async function init(action: ActionInterface): Promise { info(`Deploying using ${action.tokenType}… 🔑`) info('Configuring git…') - // Keep trying to configure Git with various different methods until it works with a maximum of 2 attempts. - const ATTEMPT_LIMIT = 2 - - let attempt = 0 - - do { - attempt++ - - if (attempt > ATTEMPT_LIMIT) { - throw new Error() - } - - if (attempt > 1) { - // Handles the 'fatal: not a git directory' error. - await execute(`git init`, action.workspace, action.silent) - + async function configureGit(throwOnError: boolean) { + try { await execute( - `git commit -m "Initial commit" --allow-empty`, + `git config --global --add safe.directory "${action.workspace}"`, action.workspace, action.silent ) + + await execute( + `git config user.name "${action.name}"`, + action.workspace, + action.silent + ) + + await execute( + `git config user.email "${action.email}"`, + action.workspace, + action.silent + ) + + await execute( + `git config core.ignorecase false`, + action.workspace, + action.silent + ) + } catch { + if (throwOnError) { + throw new Error() + } } + } + + try { + await configureGit(false) + } catch { + // Attempt to re-run if initial configuration failed using git init. + await execute(`git init`, action.workspace, action.silent) await execute( - `git config --global --add safe.directory "${action.workspace}"`, + `git commit -m "Initial commit" --allow-empty`, action.workspace, action.silent ) - - await execute( - `git config user.name "${action.name}"`, - action.workspace, - action.silent - ) - - await execute( - `git config user.email "${action.email}"`, - action.workspace, - action.silent - ) - - await execute( - `git config core.ignorecase false`, - action.workspace, - action.silent - ) - } while (attempt < ATTEMPT_LIMIT) + + await configureGit(true) + } try { if ((process.env.CI && !action.sshKey) || action.isTest) {