Skip to content

Commit

Permalink
fix(import): Handle filepaths with spaces more robustly (#1655)
Browse files Browse the repository at this point in the history
* Adds custom git prefixes for better import filepath parsing
  • Loading branch information
chrisdothtml authored and evocateur committed Sep 8, 2018
1 parent 901e6d5 commit b084293
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
Empty file.
@@ -0,0 +1,3 @@
{
"name": "files-with-spaces"
}
Empty file.
24 changes: 24 additions & 0 deletions commands/import/__tests__/import-command.test.js
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
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

0 comments on commit b084293

Please sign in to comment.