// Proves how Git.lsRemote's `patterns` match, against a real repository and // against @changesets/git's remoteTagExists, whose 4.0.2 fix (changesets#2387) // is this trap: git matches a pattern against the END of a refname at a `/`, // so a bare `pkg@1.0.0` also finds `refs/tags/@scope/pkg@1.0.0`. A fully // qualified `refs/tags/` matches that tag alone. import { execFileSync } from "node:child_process"; import { mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { remoteTagExists } from "@changesets/git"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { afterAll, assert, beforeAll, describe, it } from "@effect/vitest"; import { Effect, Layer } from "effect"; import { Git, LsRemoteEntry } from "../../src/Git.js"; const TestLayer = Git.layer.pipe(Layer.provideMerge(NodeServices.layer)); const FIXTURE_ENV = { ...process.env, LC_ALL: "C", GIT_TERMINAL_PROMPT: "0" }; const fixtureGit = (cwd: string, args: ReadonlyArray): string => execFileSync("git", args, { cwd, env: FIXTURE_ENV, encoding: "utf8" }).trim(); let root: string; const tagNames = (patterns: ReadonlyArray) => Effect.gen(function* () { const git = yield* Git; const entries = yield* git.lsRemote(root, "origin", { tags: true, patterns }); return entries.map((entry) => LsRemoteEntry.shortName(entry.ref)); }).pipe(Effect.provide(TestLayer)); beforeAll(async () => { root = await mkdtemp(join(tmpdir(), "effected-git-ls-remote-patterns-")); execFileSync("git", ["init", "--quiet", "-b", "main", root], { env: FIXTURE_ENV }); fixtureGit(root, ["config", "user.name", "Fixture"]); fixtureGit(root, ["config", "user.email", "fixture@example.invalid"]); fixtureGit(root, ["config", "commit.gpgsign", "false"]); fixtureGit(root, ["config", "tag.gpgsign", "false"]); await writeFile(join(root, "a.txt"), "a\n"); fixtureGit(root, ["add", "a.txt"]); fixtureGit(root, ["commit", "--quiet", "-m", "a"]); // The repository is its own origin, so ls-remote reads it with no network. fixtureGit(root, ["remote", "add", "origin", root]); fixtureGit(root, ["tag", "@scope/pkg@1.0.0"]); }); afterAll(async () => { await rm(root, { recursive: true, force: true }); }); describe("Git.lsRemote patterns match the end of a refname", () => { it.effect("a bare tag name also finds a scoped tag ending in it; a qualified one does not", () => Effect.gen(function* () { assert.deepStrictEqual(yield* tagNames(["pkg@1.0.0"]), ["@scope/pkg@1.0.0"]); assert.deepStrictEqual(yield* tagNames(["refs/tags/pkg@1.0.0"]), []); assert.deepStrictEqual(yield* tagNames(["refs/tags/@scope/pkg@1.0.0"]), ["@scope/pkg@1.0.0"]); // The oracle, fixed in @changesets/git 4.0.2, qualifies the name and answers the same. assert.isFalse(yield* Effect.promise(() => remoteTagExists("pkg@1.0.0", root))); assert.isTrue(yield* Effect.promise(() => remoteTagExists("@scope/pkg@1.0.0", root))); }), ); it.effect("once the bare tag exists, a qualified pattern finds it alone, as the oracle does", () => Effect.gen(function* () { fixtureGit(root, ["tag", "pkg@1.0.0"]); assert.deepStrictEqual(yield* tagNames(["refs/tags/pkg@1.0.0"]), ["pkg@1.0.0"]); assert.deepStrictEqual([...(yield* tagNames(["pkg@1.0.0"]))].sort(), ["@scope/pkg@1.0.0", "pkg@1.0.0"]); assert.isTrue(yield* Effect.promise(() => remoteTagExists("pkg@1.0.0", root))); }), ); });