Skip to content

Commit 481c036

Browse files
committed
Align glob matching with Changesets
1 parent 51e7c67 commit 481c036

4 files changed

Lines changed: 51 additions & 25 deletions

File tree

get-changed-packages.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
} from "@changesets/types";
1111
import type { Packages, Tool } from "@manypkg/get-packages";
1212
import jsYaml from "js-yaml";
13-
import micromatch from "micromatch";
13+
import picomatch from "picomatch";
1414
import type { ProbotOctokit } from "probot";
1515
import { isChangeset } from "./is-changeset.ts";
1616

@@ -60,7 +60,31 @@ function matchGlobs(
6060
): Array<string> {
6161
return paths.filter((path) => {
6262
const relativePath = nodePath.relative(cwd, path) || ".";
63-
return micromatch.isMatch(relativePath, globs);
63+
return globMatchSome([relativePath], globs);
64+
});
65+
}
66+
67+
// Mirrors https://github.com/changesets/changesets/blob/5eeb0125f2766b9458aa1725900430b27b24116e/packages/git/src/index.ts#L346-L374
68+
function globMatchSome(paths: ReadonlyArray<string>, patterns?: ReadonlyArray<string>): boolean {
69+
if (!patterns) return paths.length > 0;
70+
71+
const matchers = patterns.map((pattern) => picomatch(pattern, undefined, true));
72+
return paths.some((path) => {
73+
if (path.includes("\\")) {
74+
path = path.replaceAll("\\", "/");
75+
}
76+
77+
let passed = false;
78+
for (const matcher of matchers) {
79+
if (!passed) {
80+
if (!matcher.state.negated && matcher(path)) {
81+
passed = true;
82+
}
83+
} else if (matcher.state.negated && !matcher(path)) {
84+
passed = false;
85+
}
86+
}
87+
return passed;
6488
});
6589
}
6690

@@ -241,7 +265,7 @@ export const getChangedPackages = async ({
241265

242266
const config = parseConfig(await rawConfigPromise, packages);
243267

244-
// https://github.com/changesets/changesets/blob/6c250f58a128350c6905ed16691a25bf0c8bae0c/packages/git/src/index.ts#L264-L286
268+
// Mirrors https://github.com/changesets/changesets/blob/5eeb0125f2766b9458aa1725900430b27b24116e/packages/git/src/index.ts#L273-L304
245269
const changedPackages = packages.packages
246270
.toSorted((pkgA, pkgB) => pkgB.dir.length - pkgA.dir.length)
247271
.filter((pkg) => {
@@ -259,7 +283,7 @@ export const getChangedPackages = async ({
259283

260284
return (
261285
changedPackageFiles.length > 0 &&
262-
micromatch(changedPackageFiles, config.changedFilePatterns).length > 0
286+
globMatchSome(changedPackageFiles, config.changedFilePatterns)
263287
);
264288
})
265289
.map((pkg) => pkg.packageJson.name);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
"@sentry/node": "^6.0.0",
2929
"@types/js-yaml": "^3.12.2",
3030
"@types/markdown-table": "^2.0.0",
31-
"@types/micromatch": "^4.0.1",
3231
"@types/node": "^25.5.0",
32+
"@types/picomatch": "^4.0.3",
3333
"human-id": "^4.1.3",
3434
"js-yaml": "^3.14.0",
3535
"markdown-table": "^2.0.0",
36-
"micromatch": "^4.0.2",
36+
"picomatch": "^4.0.4",
3737
"probot": "^12.2.4",
3838
"typescript": "^6.0.2"
3939
},

pnpm-lock.yaml

Lines changed: 12 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/index.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ thing
755755
const { requests } = usePrState(server, {
756756
files: {
757757
".changeset/config.json": JSON.stringify({
758-
changedFilePatterns: ["src/**"],
758+
changedFilePatterns: ["src/**", "!src/generated/**"],
759759
}),
760760
"package.json": JSON.stringify({
761761
name: "test",
@@ -769,6 +769,13 @@ thing
769769
name: "pkg-b",
770770
}),
771771
"packages/b/src/index.ts": [{ status: "added" }, "export const b = true;"],
772+
"packages/c/package.json": JSON.stringify({
773+
name: "pkg-c",
774+
}),
775+
"packages/c/src/generated/index.ts": [
776+
{ status: "added" },
777+
"export const generated = true;",
778+
],
772779
},
773780
comments: [],
774781
});
@@ -783,6 +790,7 @@ thing
783790

784791
expect(serializedRequests).toContain("%22pkg-b%22");
785792
expect(serializedRequests).not.toContain("%22pkg-a%22");
793+
expect(serializedRequests).not.toContain("%22pkg-c%22");
786794
});
787795

788796
it("attributes changed files to the deepest matching workspace package", async ({

0 commit comments

Comments
 (0)