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(publish): Avoid errors when files are ignored by git #2445

Merged
merged 3 commits into from
May 23, 2020
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
1 change: 1 addition & 0 deletions __fixtures__/root-manifest-only/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.log
12 changes: 12 additions & 0 deletions commands/publish/__tests__/git-checkout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ test("gitCheckout files", async () => {
const modified = await execa.stdout("git", ["ls-files", "--modified"], { cwd });
expect(modified).toBe("");
});

test("gitCheckout files with .gitignored files", async () => {
const cwd = await initFixture("no-interdependencies");
const files = ["package-1", "package-2"].map(name => path.join("packages", name, "package.json"));
files.push("packages/package-1/index.log");

await Promise.all(files.map(fp => fs.writeJSON(path.join(cwd, fp), { foo: "bar" })));
await gitCheckout(files, { cwd });

const modified = await execa.stdout("git", ["ls-files", "--modified"], { cwd });
expect(modified).toBe("");
});
2 changes: 1 addition & 1 deletion commands/publish/lib/git-checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ module.exports = gitCheckout;
function gitCheckout(files, opts) {
log.silly("gitCheckout", files);

return childProcess.exec("git", ["checkout", "--"].concat(files), opts);
return childProcess.exec("git", ["checkout", "--", "."], opts);
}
26 changes: 26 additions & 0 deletions commands/version/__tests__/git-add.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,29 @@ test("absolute files", async () => {
const list = await execa.stdout("git", ["diff", "--cached", "--name-only"], { cwd });
expect(slash(list)).toBe("packages/pkg-2/index.js");
});

test(".gitignore", async () => {
const cwd = await initFixture("root-manifest-only");
const file = path.join(cwd, "packages", "pkg-2", "index.log");
const file2 = path.join(cwd, "packages", "pkg-2", "index.js");

await fs.outputFile(file, "hello");
await fs.outputFile(file2, "hello2");
await gitAdd([file, file2], { cwd });

const list = await execa.stdout("git", ["diff", "--cached", "--name-only"], { cwd });
expect(slash(list)).toBe("packages/pkg-2/index.js");
});

test(".gitignore without naming files", async () => {
const cwd = await initFixture("root-manifest-only");
const file = path.join(cwd, "packages", "pkg-2", "index.log");
const file2 = path.join(cwd, "packages", "pkg-2", "index.js");

await fs.outputFile(file, "hello");
await fs.outputFile(file2, "hello2");
await gitAdd([], { cwd });

const list = await execa.stdout("git", ["diff", "--cached", "--name-only"], { cwd });
expect(slash(list)).toBe("packages/pkg-2/index.js");
});
6 changes: 1 addition & 5 deletions commands/version/lib/git-add.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
"use strict";

const log = require("npmlog");
const path = require("path");
const slash = require("slash");
const childProcess = require("@lerna/child-process");

module.exports = gitAdd;

function gitAdd(files, opts) {
log.silly("gitAdd", files);

const filePaths = files.map(file => slash(path.relative(opts.cwd, path.resolve(opts.cwd, file))));

return childProcess.exec("git", ["add", "--", ...filePaths], opts);
return childProcess.exec("git", ["add", "--", "."], opts);
}