Skip to content

Commit

Permalink
Fix: git describe using regex instead of glob for autoVersion util (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeyadkhaled authored Feb 6, 2024
1 parent 2a8c1d9 commit 6aa8409
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
5 changes: 5 additions & 0 deletions packages/cli/changelog/@unreleased/pr-35.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Git describe using regex instead of glob for autoVersion util
links:
- https://github.com/palantir/osdk-ts/pull/35
4 changes: 2 additions & 2 deletions packages/cli/src/util/autoVersion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe("autoVersion", () => {

expect(version).toBe("1.2.3");
expect(execSyncMock).toHaveBeenCalledWith(
"git describe --tags --first-parent --dirty --match=\"/^@package@/*\"",
"git describe --tags --first-parent --dirty --match=\"@package@*\"",
{ encoding: "utf8" },
);
});
Expand All @@ -69,7 +69,7 @@ describe("autoVersion", () => {

expect(version).toBe("1.2.3-package");
expect(execSyncMock).toHaveBeenCalledWith(
"git describe --tags --first-parent --dirty --match=\"/^-package/*\"",
"git describe --tags --first-parent --dirty --match=\"-package*\"",
{ encoding: "utf8" },
);
});
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/src/util/autoVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ import { isValidSemver } from "./isValidSemver.js";
* @throws An error if the version string is not SemVer compliant or if the version cannot be determined.
*/
export async function autoVersion(tagPrefix: string = ""): Promise<string> {
const matchRegExp = new RegExp(tagPrefix == "" ? "^v?" : `^${tagPrefix}`);
const matchClause = tagPrefix != "" ? ` --match="${matchRegExp}*"` : "";
const [matchPrefix, prefixRegex] = tagPrefix !== ""
? [tagPrefix, new RegExp(`^${tagPrefix}`)]
: [undefined, new RegExp(`^v?`)];
try {
const gitVersion = execSync(
`git describe --tags --first-parent --dirty${matchClause}`,
`git describe --tags --first-parent --dirty${
matchPrefix != null ? ` --match="${matchPrefix}*"` : ""
}`,
{ encoding: "utf8" },
);
const version = gitVersion.trim().replace(matchRegExp, "");
const replaceRegExp = new RegExp(prefixRegex);
const version = gitVersion.trim().replace(replaceRegExp, "");
if (!isValidSemver(version)) {
throw new Error(`The version string ${version} is not SemVer compliant.`);
}
Expand Down

0 comments on commit 6aa8409

Please sign in to comment.