rsnext/packages/create-next-app/helpers/git.ts
Dinesh Balaji 19febb10c0
feat: update create-next-app to init with main as initial branch (#17745)
When `create-next-app` is initialized a git repo, it now uses 'main' as the initial branch.

The branch master does not actually exist initially. The branches don't get created only when they have at least one commit. Until the branch gets created, the branch only exists in .git/HEAD. So there is no master branch initialized in the repo.

Closes: https://github.com/vercel/next.js/issues/17733

<img width="639" alt="Screenshot 2020-10-09 at 17 26 30" src="https://user-images.githubusercontent.com/4656109/95580229-9f3c6c80-0a54-11eb-967f-180eb9601c1a.png">
2020-11-07 14:46:17 +00:00

48 lines
1.1 KiB
TypeScript

/* eslint-disable import/no-extraneous-dependencies */
import { execSync } from 'child_process'
import path from 'path'
import rimraf from 'rimraf'
function isInGitRepository(): boolean {
try {
execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' })
return true
} catch (_) {}
return false
}
function isInMercurialRepository(): boolean {
try {
execSync('hg --cwd . root', { stdio: 'ignore' })
return true
} catch (_) {}
return false
}
export function tryGitInit(root: string): boolean {
let didInit = false
try {
execSync('git --version', { stdio: 'ignore' })
if (isInGitRepository() || isInMercurialRepository()) {
return false
}
execSync('git init', { stdio: 'ignore' })
didInit = true
execSync('git checkout -b main', { stdio: 'ignore' })
execSync('git add -A', { stdio: 'ignore' })
execSync('git commit -m "Initial commit from Create Next App"', {
stdio: 'ignore',
})
return true
} catch (e) {
if (didInit) {
try {
rimraf.sync(path.join(root, '.git'))
} catch (_) {}
}
return false
}
}