From 34714a3ac12546822a08cd4ee0be9a388c5d384b Mon Sep 17 00:00:00 2001 From: shmck Date: Sun, 13 Jun 2021 20:23:48 -0700 Subject: [PATCH] fix git issue pre-v2.28 due to master->main change Signed-off-by: shmck --- src/services/git/index.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/services/git/index.ts b/src/services/git/index.ts index d2a7d923..0e72e2d8 100644 --- a/src/services/git/index.ts +++ b/src/services/git/index.ts @@ -1,5 +1,6 @@ import * as TT from 'typings/tutorial' import { exec, exists } from '../node' +import { version, compareVersions } from '../dependencies' import logger from '../logger' export const gitOrigin = 'coderoad' @@ -70,8 +71,22 @@ export async function clear(): Promise { } async function init(): Promise { + const gitVersion = await version('git') + if (!gitVersion) { + throw new Error('Error: No git version found') + } + const hasInitialBranch = await compareVersions(gitVersion, '>=2.28.0') + let stderr + if (hasInitialBranch) { + // --initial-branch is introduced in git v2.28 when git changed the default master -> main + const initResult = await exec({ command: 'git init --initial-branch=master' }) + stderr = initResult.stderr + } else { + // pre git v2.28, master is default branch + const initResult = await exec({ command: 'git init' }) + stderr = initResult.stderr + } // note: prevents stderr warning concerning default init branch - const { stderr } = await exec({ command: 'git init --initial-branch=master' }) if (stderr) { throw new Error(`Error initializing Git: ${stderr}`) }