Skip to content

Commit

Permalink
Get correct absolute path of changed files when .changeset/ is not …
Browse files Browse the repository at this point in the history
…in the root of the repository (#770)

* Get repository root instead of `cwd`

* Create silly-sloths-brush.md

* Add full path test case

* Update .changeset/silly-sloths-brush.md

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>

* Add `@changesets/cli` changeset

* Throw instead of returning `cwd`

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
alizeait and Andarist committed Mar 13, 2022
1 parent 5f0f456 commit eb86652
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/sharp-tools-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/cli": patch
---

`changeset add` and `changeset status` should now properly handle the situation where Changesets are managed from a directory different than the root of the repository.
5 changes: 5 additions & 0 deletions .changeset/silly-sloths-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/git": patch
---

`getChangedFilesSince` and `getChangedPackagesSinceRef` will now return the correct absolute paths of the changed files when the passed `cwd` is different from the repository's root.
34 changes: 34 additions & 0 deletions packages/git/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "path";
import fixtures from "fixturez";
import spawn from "spawndamnit";
import fileUrl from "file-url";
Expand Down Expand Up @@ -416,6 +417,39 @@ describe("git", () => {
"packages/pkg-b/package.json"
);
});
it("should get correct full paths of changed files irrespective of cwd", async () => {
const ref = await spawn("git", ["rev-parse", "HEAD"], { cwd });

await add("packages/pkg-a/index.js", cwd);
await commit("Add packageA index", cwd);

await add("packages/pkg-b/index.js", cwd);
await commit("Added packageB index", cwd);

const filesChangedSinceRef = await getChangedFilesSince({
ref: ref.stdout.toString().trim(),
cwd,
fullPath: true
});
expect(filesChangedSinceRef[0]).toBe(
path.resolve(cwd, "packages/pkg-a/index.js")
);
expect(filesChangedSinceRef[1]).toBe(
path.resolve(cwd, "packages/pkg-b/index.js")
);

const filesChangedSinceRef2 = await getChangedFilesSince({
ref: ref.stdout.toString().trim(),
cwd: path.resolve(cwd, "packages"),
fullPath: true
});
expect(filesChangedSinceRef2[0]).toBe(
path.resolve(cwd, "packages/pkg-a/index.js")
);
expect(filesChangedSinceRef2[1]).toBe(
path.resolve(cwd, "packages/pkg-b/index.js")
);
});
});

describe("getChangedPackagesSinceRef", () => {
Expand Down
20 changes: 19 additions & 1 deletion packages/git/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,22 @@ export async function isRepoShallow({ cwd }: { cwd: string }) {
export async function deepenCloneBy({ by, cwd }: { by: number; cwd: string }) {
await spawn("git", ["fetch", `--deepen=${by}`], { cwd });
}
async function getRepoRoot({ cwd }: { cwd: string }) {
const { stdout, code, stderr } = await spawn(
"git",
["rev-parse", "--show-toplevel"],
{ cwd }
);

if (code !== 0) {
throw new Error(stderr.toString());
}

return stdout
.toString()
.trim()
.replace(/\n|\r/g, "");
}

export async function getChangedFilesSince({
cwd,
Expand All @@ -207,7 +223,9 @@ export async function getChangedFilesSince({
.split("\n")
.filter(a => a);
if (!fullPath) return files;
return files.map(file => path.resolve(cwd, file));

const repoRoot = await getRepoRoot({ cwd });
return files.map(file => path.resolve(repoRoot, file));
}

// below are less generic functions that we use in combination with other things we are doing
Expand Down

0 comments on commit eb86652

Please sign in to comment.