Skip to content

Commit

Permalink
fix: handle absolute paths correctly
Browse files Browse the repository at this point in the history
Fixes #1.
  • Loading branch information
motiz88 committed May 10, 2017
1 parent 893b21e commit b2193ea
Show file tree
Hide file tree
Showing 7 changed files with 483 additions and 27 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
"mock-bin": "^1.0.1",
"mock-git": "^1.0.3",
"mz": "^2.6.0",
"tmp-promise": "^1.0.3",
"semantic-release": "^6.3.6"
"pmock": "^0.2.3",
"semantic-release": "^6.3.6",
"tmp-promise": "^1.0.3"
},
"engines": {
"node": ">=6",
Expand Down
3 changes: 3 additions & 0 deletions src/__mocks__/TestEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import mockBin from "mock-bin";
import tmp from "tmp-promise";
import path from "path";
import fs from "mz/fs";
import pmock from "pmock";

export default class TestEnvironment {
_unmock = [];
Expand Down Expand Up @@ -53,6 +54,8 @@ export default class TestEnvironment {
async setup() {
this._spyLogFile = await tmp.tmpName();
this._unmock.push(await mockGit(this._makeSpyJs("git"), "add"));
const cwdMock = pmock.cwd("/path/to/repo");
this._unmock.push(() => cwdMock.reset());
await this.setAllStagedFiles();
await this.setPartialFiles();
}
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ Array [
]
`;
exports[`git-exec-and-restage with command+args and mixed status, absolute paths 1`] = `
Array [
"git diff --name-only --diff-filter=ACDMRTUXB fullystaged.js partiallystaged.js",
"prettier --write /path/to/repo/fullystaged.js /path/to/repo/partiallystaged.js",
"git add fullystaged.js",
]
`;
exports[`git-exec-and-restage with command+args, files implicit 1`] = `
Array [
"git diff-index --cached --name-only --diff-filter=ACDMRTUXB HEAD",
Expand Down
16 changes: 16 additions & 0 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import gitExecAndRestage from "../";
import TestEnvironment from "TestEnvironment";
import path from "path";

describe("git-exec-and-restage", () => {
const env = new TestEnvironment();
Expand All @@ -20,11 +21,13 @@ describe("git-exec-and-restage", () => {
});
it("with command, arguments, fully staged file, no partials", async () => {
await env.mockBin("prettier");
await env.setAllStagedFiles(["fullystaged.js"]);
await gitExecAndRestage(["prettier", "--write", "--", "fullystaged.js"]);
expect(await env.log).toMatchSnapshot();
});
it("with command+args, mix of fully and partially staged files", async () => {
await env.mockBin("prettier");
await env.setAllStagedFiles(["fullystaged.js", "partiallystaged.js"]);
await env.setPartialFiles(["partiallystaged.js"]);
await gitExecAndRestage([
"prettier",
Expand Down Expand Up @@ -54,4 +57,17 @@ describe("git-exec-and-restage", () => {
await gitExecAndRestage(["prettier", "--write", "--", "fullystaged1.js"]);
expect(await env.log).toMatchSnapshot();
});
it("with command+args and mixed status, absolute paths", async () => {
await env.mockBin("prettier");
await env.setAllStagedFiles(["fullystaged.js", "partiallystaged.js"]);
await env.setPartialFiles(["partiallystaged.js"]);
await gitExecAndRestage([
"prettier",
"--write",
"--",
path.join(process.cwd(), "fullystaged.js"),
path.join(process.cwd(), "partiallystaged.js")
]);
expect(await env.log).toMatchSnapshot();
});
});
39 changes: 35 additions & 4 deletions src/gitUtils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import spawn from "cross-spawn-promise";
import path from "path";

function ensureRelativePath(s) {
if (path.isAbsolute(s)) {
return path.relative(process.cwd(), s);
}
return s;
}

async function getAllStaged() {
const diffOut =
Expand Down Expand Up @@ -28,13 +36,36 @@ async function getFullyStaged(
const diffOut =
(await spawn(
"git",
["diff", "--name-only", "--diff-filter=ACDMRTUXB", ...files],
[
"diff",
"--name-only",
"--diff-filter=ACDMRTUXB",
...files.map(filename => ensureRelativePath(filename))
],
{
encoding: "utf8"
}
)) || "";
const notFullyStaged = new Set(diffOut.split("\n").filter(s => s !== ""));
return files.filter(file => !notFullyStaged.has(file));
const notFullyStaged = new Set(
diffOut
.split("\n")
.filter(s => s !== "")
.map(filename => path.resolve(process.cwd(), filename))
);
return files.filter(
file => !notFullyStaged.has(path.resolve(process.cwd(), file))
);
}

async function stageFiles(
/* istanbul ignore next: convenience default */
files = []
) {
await spawn(
"git",
["add", ...files.map(filename => ensureRelativePath(filename))],
{ stdio: "inherit" }
);
}

export { getAllStaged, getFullyStaged };
export { getAllStaged, getFullyStaged, stageFiles };
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import spawn from "cross-spawn-promise";
import CliError from "./CliError";
import { getAllStaged, getFullyStaged } from "./gitUtils";
import { getAllStaged, getFullyStaged, stageFiles } from "./gitUtils";

export { CliError };

Expand All @@ -18,7 +18,7 @@ async function main(argv = []) {
const fullyStaged = await getFullyStaged(files);
await spawn(command, commandArgs.concat(files), { stdio: "inherit" });
if (fullyStaged.length) {
await spawn("git", ["add", ...fullyStaged], { stdio: "inherit" });
await stageFiles(fullyStaged);
}
}

Expand Down

1 comment on commit b2193ea

@motiz88
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes #2 (misstated in original commit message).

Please sign in to comment.