Skip to content

Commit

Permalink
Fix provided by 'git-version'
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattheis committed Aug 10, 2022
1 parent 65a65e1 commit 171006f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
12 changes: 12 additions & 0 deletions changelog/209-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
type: fix
issue: 209
audience: user
components:
- server
---

# Fix provided by 'git-version'

Snage will now find the first matching tag instead of aborting the search if the
first tag doesn't match the version regex.
1 change: 1 addition & 0 deletions eslint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ rules:
default-param-last: error
no-loop-func: off
arrow-body-style: [error, as-needed]
no-constant-condition: off

unicorn/no-abusive-eslint-disable: error
unicorn/no-array-instanceof: error
Expand Down
38 changes: 27 additions & 11 deletions packages/snage/src/provider/git-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const getVersion = (
): TE.TaskEither<string, FieldValue | undefined> =>
pipe(
getFirstTagContainingFile(file, versionRegex),
TE.map(O.map((tag) => extractVersion(tag, versionRegex))),
TE.mapLeft((error): string => `provider error on field '${field.name}': ${error}`),
TE.chainEitherK(
O.fold(
Expand Down Expand Up @@ -58,14 +57,31 @@ const fetchCommit = (directory: string, filename: string): TE.TaskEither<string,
TE.map(O.fromPredicate((commit) => typeof commit !== 'undefined' && commit !== ''))
);

const getTag = (directory: string, commit: string, versionRegex: string): TE.TaskEither<string, O.Option<string>> =>
pipe(
tryExec(`git name-rev --tags --name-only "${commit}"`, {cwd: directory}),
TE.map((tag) => tag.trim()),
TE.map(O.fromPredicate((tag) => typeof tag !== 'undefined' && tag !== '' && tag !== 'undefined')),
TE.map(O.map((rawTag) => rawTag.replace(/[~^].*/, ''))),
TE.map(O.filter((tag) => RegExp(versionRegex).test(tag)))
);
const getTag =
(directory: string, commit: string, versionRegex: string): TE.TaskEither<string, O.Option<string>> =>
async (): Promise<E.Either<string, O.Option<string>>> => {
const excludes: string[] = [];
while (true) {
const result = await pipe(
tryExec(`git name-rev --tags ${excludes.join(' ')} --name-only "${commit}"`, {cwd: directory}),
TE.map((tag) => tag.trim())
)();

const extractVersion = (tag: string, versionRegex: string): string =>
tag.replace(RegExp(versionRegex), (match, version) => version);
if (E.isLeft(result)) {
return result;
}
const rawTag = result.right;
// name-rev returns the string undefined when there are no tags left
if (typeof rawTag === 'undefined' || rawTag === '' || rawTag === 'undefined') {
return E.right(O.none);
}
const tag = rawTag.replace(/[~^].*/, '');
if (tag) {
const match = RegExp(versionRegex).exec(tag);
if (match) {
return E.right(O.some(match[1]));
}
excludes.push(`--exclude ${tag}`);
}
}
};

0 comments on commit 171006f

Please sign in to comment.