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

Ejecting should ensure you have clean git status #2090

Closed
wants to merge 15 commits into from
Closed
Changes from 7 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
26 changes: 26 additions & 0 deletions packages/react-scripts/scripts/eject.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const path = require('path');
const spawnSync = require('cross-spawn').sync;
const chalk = require('chalk');
const prompt = require('react-dev-utils/prompt');
const execSync = require('child_process').execSync;
const paths = require('../config/paths');
const createJestConfig = require('./utils/createJestConfig');

Expand All @@ -36,6 +37,31 @@ prompt(
process.exit(1);
}

// Make sure there are no dirty git status
function statusSync() {
try {
let stdout = execSync(`git status --porcelain`).toString();
let status = { dirty: 0, untracked: 0 };
Copy link

Choose a reason for hiding this comment

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

If you're also counting untracked files, why isn't that used in the dirty check below? If it shouldn't be used, why is it being counted?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right, I think untracked files should be exclude. I will make pull request on this tonight.

stdout.trim().split(/\r?\n/).forEach(file => {
Copy link
Contributor

Choose a reason for hiding this comment

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

You might want to use git status --porcelain for easier parsing.

if (file.substr(0, 2) === '??') status.untracked++;
else status.dirty++;
});
return status;
} catch (e) {
return false;
}
}

const status = statusSync();
if (status && status.dirty) {
console.error(
`Git repository has ${status.dirty} dirty ${status.dirty > 1 ? 'files' : 'file'}. ` +
'We cannot continue as you would lose all the changes in that file or directory. ' +
'Please push commit before and run this command again.'
Copy link

Choose a reason for hiding this comment

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

I think this error message covers all the right bases. The grammar however is a bit confusing.

The first sentence mentions that you have 1 or more dirty files, but then the second sentence only refers to a single file or directory. The third sentence isn't totally clear grammatically and it states that you need to push as well as commit in order to make the directory clean. While pushing may be ideal, it isn't the same as committing and only committing is really necessary here.

What do you think of this as the error message instead?

`This git repository has ${status.dirty} dirty ${status.dirty > 1 ? 'files' : 'file'}. ` +
'We cannot continue as you would lose all of your changes. ' +
'Please commit your changes with `git commit` and then run this command again.'

This way, the user knows what to do to fix the problem and the message is completely clear.

Copy link

@taylorlapeyre taylorlapeyre May 10, 2017

Choose a reason for hiding this comment

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

My attempt at the grammar on this:

`This git repository has ${status.dirty} ${status.dirty > 1 ? 'files' : 'file'} with uncommitted changes.` +
'Ejecting would cause these files to be overwritten. ' +
'Please commit your changes with `git commit` and then run this command again.'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for those suggestions @sunjay, @taylorlapeyre. I personally liked @taylorlapeyre version. I will make an update soon.

);
process.exit(1);
}

console.log('Ejecting...');

const ownPath = paths.ownPath;
Expand Down