()
| 71 | } |
| 72 | |
| 73 | async function init(): Promise<Error | void> { |
| 74 | const { version: gitVersion, error: gitError } = await getVersion('git') |
| 75 | if (gitError) { |
| 76 | throw new Error(`Error: Git config error: ${gitError.message}`) |
| 77 | } |
| 78 | if (!gitVersion) { |
| 79 | throw new Error('Error: No git version found') |
| 80 | } |
| 81 | const hasInitialBranch = await compareVersions(gitVersion, '>=2.28.0') |
| 82 | let stderr |
| 83 | if (hasInitialBranch) { |
| 84 | // --initial-branch is introduced in git v2.28 when git changed the default master -> main |
| 85 | const initResult = await exec({ command: 'git init --initial-branch=master' }) |
| 86 | stderr = initResult.stderr |
| 87 | } else { |
| 88 | // pre git v2.28, master is default branch |
| 89 | const initResult = await exec({ command: 'git init' }) |
| 90 | stderr = initResult.stderr |
| 91 | } |
| 92 | // note: prevents stderr warning concerning default init branch |
| 93 | if (stderr) { |
| 94 | throw new Error(`Error initializing Git: ${stderr}`) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | export async function initIfNotExists(): Promise<never | void> { |
| 99 | const hasGitInit = exists('.git') |
no test coverage detected