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

fix(import): Handle filepaths with spaces more robustly #1655

Merged
merged 4 commits into from
Sep 8, 2018
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "files-with-spaces"
}
24 changes: 24 additions & 0 deletions commands/import/__tests__/import-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,30 @@ describe("ImportCommand", () => {
expect(await pathExists(newFilePath)).toBe(true);
});

it("supports filepaths that have spaces within the external repo", async () =>
Promise.all(
// running the same test with and without --flatten
[true, false].map(async shouldFlatten => {
const [testDir, externalDir] = await Promise.all([
initFixture("basic"),
initFixture("files-with-spaces", "Init external commit"),
]);
const newPackagePath = path.join(testDir, "packages", path.basename(externalDir));
const newFilePath = path.join(newPackagePath, "file with spaces");
const newDeepFilePath = path.resolve(newPackagePath, "subfolder b/file");

if (shouldFlatten) {
await lernaImport(testDir)(externalDir, "--flatten");
} else {
await lernaImport(testDir)(externalDir);
}

expect(await lastCommitInDir(testDir)).toBe("Init external commit");
expect(await pathExists(newFilePath)).toBe(true);
expect(await pathExists(newDeepFilePath)).toBe(true);
})
));

it("skips empty patches with --flatten", async () => {
const [testDir, externalDir] = await initBasicFixtures();
const filePath = path.join(externalDir, "file.txt");
Expand Down
19 changes: 15 additions & 4 deletions commands/import/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,23 @@ class ImportCommand extends Command {
"--binary",
"-1",
sha,
// custom git prefixes for accurate parsing of filepaths (#1655)
`--src-prefix=COMPARE_A/`,
`--dst-prefix=COMPARE_B/`,
]);
const version = this.externalExecSync("git", ["--version"]).replace(/git version /g, "");

patch = `${diff}\n--\n${version}`;
} else {
patch = this.externalExecSync("git", ["format-patch", "-1", sha, "--stdout"]);
patch = this.externalExecSync("git", [
"format-patch",
"-1",
sha,
"--stdout",
// custom git prefixes for accurate parsing of filepaths (#1655)
`--src-prefix=COMPARE_A/`,
`--dst-prefix=COMPARE_B/`,
]);
}

const formattedTarget = this.targetDirRelativeToGitRoot.replace(/\\/g, "/");
Expand All @@ -156,9 +167,9 @@ class ImportCommand extends Command {
// to all affected files. This moves the git history for the entire
// external repository into the package subdirectory, commit by commit.
return patch
.replace(/^([-+]{3} [ab])/gm, replacement)
.replace(/^(diff --git a)/gm, replacement)
.replace(/^(diff --git \S+ b)/gm, replacement)
.replace(/^([-+]{3} COMPARE_[AB])/gm, replacement)
.replace(/^(diff --git COMPARE_A)/gm, replacement)
.replace(/^(diff --git (?! COMPARE_B\/).+ COMPARE_B)/gm, replacement)
.replace(/^(copy (from|to)) /gm, `$1 ${formattedTarget}/`)
.replace(/^(rename (from|to)) /gm, `$1 ${formattedTarget}/`);
}
Expand Down