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 command so it works if Lerna root is a subdir of git root #1199

Merged
merged 1 commit into from
Jan 16, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/GitUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ export default class GitUtilities {
return diff;
}

static getWorkspaceRoot(opts) {
log.silly("getWorkspaceRoot");
const root = ChildProcessUtilities.execSync("git", ["rev-parse", "--show-toplevel"], opts);
log.verbose("getWorkspaceRoot", root);

return root;
}

static getCurrentBranch(opts) {
log.silly("getCurrentBranch");

Expand Down
17 changes: 12 additions & 5 deletions src/commands/ImportCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,17 @@ export default class ImportCommand extends Command {
}

const targetBase = getTargetBase(this.repository.packageConfigs);
this.targetDir = path.join(targetBase, externalRepoBase);

if (FileSystemUtilities.existsSync(path.resolve(this.repository.rootPath, this.targetDir))) {
return callback(new Error(`Target directory already exists "${this.targetDir}"`));
// Compute a target directory relative to the Lerna root
const targetDir = path.join(targetBase, externalRepoBase);

// Compute a target directory relative to the Git root
const gitRepoRoot = GitUtilities.getWorkspaceRoot(this.execOpts);
const lernaRootRelativeToGitRoot = path.relative(gitRepoRoot, this.repository.rootPath);
this.targetDirRelativeToGitRoot = path.join(lernaRootRelativeToGitRoot, targetDir);

if (FileSystemUtilities.existsSync(path.resolve(this.repository.rootPath, targetDir))) {
return callback(new Error(`Target directory already exists "${targetDir}"`));
}

this.commits = this.externalExecSync("git", this.gitParamsForTargetCommits())
Expand All @@ -103,7 +110,7 @@ export default class ImportCommand extends Command {

this.logger.info(
"",
`About to import ${this.commits.length} commits from ${inputPath} into ${this.targetDir}`,
`About to import ${this.commits.length} commits from ${inputPath} into ${targetDir}`,
);

if (this.options.yes) {
Expand Down Expand Up @@ -142,7 +149,7 @@ export default class ImportCommand extends Command {
patch = this.externalExecSync("git", ["format-patch", "-1", sha, "--stdout"]);
}

const formattedTarget = this.targetDir.replace(/\\/g, "/");
const formattedTarget = this.targetDirRelativeToGitRoot.replace(/\\/g, "/");
const replacement = `$1/${formattedTarget}`;
// Create a patch file for this commit and prepend the target directory
// to all affected files. This moves the git history for the entire
Expand Down
9 changes: 9 additions & 0 deletions test/GitUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ describe("GitUtilities", () => {
});
});

describe(".getWorkspaceRoot()", () => {
it("calls `git rev-parse --show-toplevel`", () => {
ChildProcessUtilities.execSync.mockImplementation(() => "master");
const opts = { cwd: "test" };
expect(GitUtilities.getWorkspaceRoot(opts)).toBe("master");
expect(ChildProcessUtilities.execSync).lastCalledWith("git", ["rev-parse", "--show-toplevel"], opts);
});
});

describe(".getCurrentBranch()", () => {
it("calls `git rev-parse --abbrev-ref HEAD`", () => {
ChildProcessUtilities.execSync.mockImplementation(() => "master");
Expand Down
29 changes: 29 additions & 0 deletions test/ImportCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,33 @@ describe("ImportCommand", () => {
expect(await pathExists(newFilePath)).toBe(true);
});
});

describe("with non-root Lerna dir", () => {
let testDir;
let lernaRootDir;
let externalDir;
let lernaImport;

beforeEach(async () => {
const [extDir, fixtureDir] = await Promise.all([
initFixture("ImportCommand/external", "Init external commit"),
initFixture("ImportCommand/lerna-not-in-root"),
]);

externalDir = extDir;
testDir = fixtureDir;
lernaRootDir = path.join(testDir, "subdir");
lernaImport = run(lernaRootDir);
});

// Issue 1197
it("creates a module in packages location with imported commit history", async () => {
const packageJson = path.join(lernaRootDir, "packages", path.basename(externalDir), "package.json");

await lernaImport(externalDir);

expect(await lastCommitInDir(testDir)).toBe("Init external commit");
expect(await pathExists(packageJson)).toBe(true);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"lerna": "__TEST_VERSION__",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "independent"
}
Empty file.