Skip to content

Commit

Permalink
fix(publish): Avoid errors when files are ignored by git (#2445)
Browse files Browse the repository at this point in the history
Fixes #2151 

Co-authored-by: Jamie Martin <jamie.martin@fireeye.com>
  • Loading branch information
sunlanterns and Jamie Martin committed May 23, 2020
1 parent c74ffa4 commit 448f2ae
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions __fixtures__/root-manifest-only/.gitignore
@@ -0,0 +1 @@
*.log
@@ -0,0 +1 @@
*.log
12 changes: 12 additions & 0 deletions commands/publish/__tests__/git-checkout.test.js
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
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
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
@@ -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);
}

0 comments on commit 448f2ae

Please sign in to comment.