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

Get correct absolute path of changed files when .changeset/ is not in the root of the repository #770

Merged
merged 7 commits into from
Mar 13, 2022
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
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