1 file changed +16
-1
lines changed Original file line number Diff line number Diff line change 1
1
import * as TT from 'typings/tutorial'
2
2
import { exec , exists } from '../node'
3
+ import { version , compareVersions } from '../dependencies'
3
4
import logger from '../logger'
4
5
5
6
export const gitOrigin = 'coderoad'
@@ -70,8 +71,22 @@ export async function clear(): Promise<Error | void> {
70
71
}
71
72
72
73
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
+ }
73
89
// note: prevents stderr warning concerning default init branch
74
- const { stderr } = await exec ( { command : 'git init --initial-branch=master' } )
75
90
if ( stderr ) {
76
91
throw new Error ( `Error initializing Git: ${ stderr } ` )
77
92
}
0 commit comments