Skip to content

fix git issue pre-v2.28 due to master->main change #496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/services/git/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -70,8 +71,22 @@ export async function clear(): Promise<Error | void> {
}

async function init(): Promise<Error | void> {
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}`)
}
Expand Down