Skip to content
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

Run git init before template dependencies are installed #8282

Merged
merged 1 commit into from
Jan 31, 2020
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
50 changes: 33 additions & 17 deletions packages/react-scripts/scripts/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,41 @@ function isInMercurialRepository() {
}
}

function tryGitInit(appPath) {
let didInit = false;
function tryGitInit() {
try {
execSync('git --version', { stdio: 'ignore' });
if (isInGitRepository() || isInMercurialRepository()) {
return false;
}

execSync('git init', { stdio: 'ignore' });
didInit = true;
return true;
} catch (e) {
console.warn('Git repo not initialized', e);
return false;
}
}

function tryGitCommit(appPath) {
try {
execSync('git add -A', { stdio: 'ignore' });
execSync('git commit -m "Initial commit from Create React App"', {
stdio: 'ignore',
});
return true;
} catch (e) {
if (didInit) {
// If we successfully initialized but couldn't commit,
// maybe the commit author config is not set.
// In the future, we might supply our own committer
// like Ember CLI does, but for now, let's just
// remove the Git files to avoid a half-done state.
try {
// unlinkSync() doesn't work on directories.
fs.removeSync(path.join(appPath, '.git'));
} catch (removeErr) {
// Ignore.
}
// We couldn't commit in already initialized git repo,
// maybe the commit author config is not set.
// In the future, we might supply our own committer
// like Ember CLI does, but for now, let's just
// remove the Git files to avoid a half-done state.
console.warn('Git commit not created', e);
console.warn('Removing .git directory...');
try {
// unlinkSync() doesn't work on directories.
fs.removeSync(path.join(appPath, '.git'));
} catch (removeErr) {
// Ignore.
}
return false;
}
Expand Down Expand Up @@ -206,6 +212,15 @@ module.exports = function(
);
}

// Initialize git repo
let initializedGit = false;

if (tryGitInit()) {
initializedGit = true;
console.log();
console.log('Initialized a git repository.');
}

let command;
let remove;
let args;
Expand Down Expand Up @@ -265,9 +280,10 @@ module.exports = function(
return;
}

if (tryGitInit(appPath)) {
// Create git commit if git repo was initialized
if (initializedGit && tryGitCommit(appPath)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably break out tryGitCommit into another line and call it didCommit or something... but I think it's OK like this too.

console.log();
console.log('Initialized a git repository.');
console.log('Created git commit.');
}

// Display the most elegant way to cd.
Expand Down