Skip to content

Commit f287d5d

Browse files
authoredJun 14, 2021
Merge pull request #496 from coderoad/fix/early-git-version
fix git issue pre-v2.28 due to master->main change
2 parents df5c95e + 34714a3 commit f287d5d

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed
 

‎src/services/git/index.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as TT from 'typings/tutorial'
22
import { exec, exists } from '../node'
3+
import { version, compareVersions } from '../dependencies'
34
import logger from '../logger'
45

56
export const gitOrigin = 'coderoad'
@@ -70,8 +71,22 @@ export async function clear(): Promise<Error | void> {
7071
}
7172

7273
async function init(): Promise<Error | void> {
74+
const gitVersion = await version('git')
75+
if (!gitVersion) {
76+
throw new Error('Error: No git version found')
77+
}
78+
const hasInitialBranch = await compareVersions(gitVersion, '>=2.28.0')
79+
let stderr
80+
if (hasInitialBranch) {
81+
// --initial-branch is introduced in git v2.28 when git changed the default master -> main
82+
const initResult = await exec({ command: 'git init --initial-branch=master' })
83+
stderr = initResult.stderr
84+
} else {
85+
// pre git v2.28, master is default branch
86+
const initResult = await exec({ command: 'git init' })
87+
stderr = initResult.stderr
88+
}
7389
// note: prevents stderr warning concerning default init branch
74-
const { stderr } = await exec({ command: 'git init --initial-branch=master' })
7590
if (stderr) {
7691
throw new Error(`Error initializing Git: ${stderr}`)
7792
}

0 commit comments

Comments
 (0)
Failed to load comments.