From 7492860ffb3a010ff2273abf45c7414c098bdc37 Mon Sep 17 00:00:00 2001 From: Nicolas Cuillery Date: Thu, 1 Feb 2018 13:59:57 -0800 Subject: [PATCH] Keep the ignored files during the upgrade process MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: This PR fixes the issue #12237 relative to react-native-git-upgrade. When the user adds new files to the .gitignore file, those files are deleted during the upgrade process because it starts by creating a fresh .gitignore file without user's modification, so the user's ignored files are no longer ignored and they are deleted during the upgrading process. The best solution I've found to keep the user's ignored files... ignored, consists in appending the content of the .gitignore file in the `/.git-rn/info/exclude` file. The user's ignored files are now ignored at the repository level, they no longer interfere with the upgrade process. Reminder: The tool uses a temporary local Git repository generated on the fly, so we can do whatever we want in it. - Publish react-native-git-upgrade to sinopia - `npm install -g react-native-git-upgrade` - Init a new project with an old version: `react-native init MyApp --version=0.50.0` - Create a new file named `dummy-ignored-file.json` in the project's root - Append `dummy-ignored-file.json` to the `.gitignore` file - Run `react-native-git-upgrade` 👉 The `dummy-ignored-file.json` is not deleted. [CLI][BUGFIX][react-native-git-upgrade] - Keep the user's ignored files while upgrading Closes https://github.com/facebook/react-native/pull/17393 Differential Revision: D6839856 Pulled By: hramos fbshipit-source-id: e4e9d759b59790e3cbc52408cef8314bf0a9c772 --- react-native-git-upgrade/cliEntry.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/react-native-git-upgrade/cliEntry.js b/react-native-git-upgrade/cliEntry.js index b65c23733ddf3e..1bb36cc63d5224 100644 --- a/react-native-git-upgrade/cliEntry.js +++ b/react-native-git-upgrade/cliEntry.js @@ -147,6 +147,28 @@ function configureGitEnv(tmpDir) { process.env.GIT_WORK_TREE = '.'; } +function copyCurrentGitIgnoreFile(tmpDir) { + /* + * The user may have added new files or directories in the .gitignore file. + * We need to keep those files ignored during the process, otherwise they + * will be deleted. + * See https://github.com/facebook/react-native/issues/12237 + */ + try { + const gitignorePath = path.resolve(process.cwd(), '.gitignore'); + const repoExcludePath = path.resolve(tmpDir, process.env.GIT_DIR, 'info/exclude'); + const content = fs.readFileSync(gitignorePath, 'utf8'); + fs.appendFileSync(repoExcludePath, content); + } catch (err) { + if (err.code === 'ENOENT') { + log.info('No .gitignore file found, this step is a no-op'); + return; + } + + throw err; + } +} + function generateTemplates(generatorDir, appName, verbose) { try { const yeomanGeneratorEntryPoint = path.resolve(generatorDir, 'index.js'); @@ -277,9 +299,12 @@ async function run(requestedVersion, cliArgs) { log.info('Configure Git environment'); configureGitEnv(tmpDir); - log.info('Init Git repository'); + log.info('Init temporary Git repository'); await exec('git init', verbose); + log.info('Save current .gitignore file'); + copyCurrentGitIgnoreFile(tmpDir); + log.info('Add all files to commit'); await exec('git add .', verbose);