diff --git a/lib/datasource/git-refs/index.spec.ts b/lib/datasource/git-refs/index.spec.ts index 46d3b7833dda614c303772f222bf590896986680..e3f2b43b6d8692056feb95f4d6a97230f2ca0f8e 100644 --- a/lib/datasource/git-refs/index.spec.ts +++ b/lib/datasource/git-refs/index.spec.ts @@ -1,5 +1,5 @@ import _simpleGit from 'simple-git/promise'; -import { getReleases } from '.'; +import { getDigest, getReleases } from '.'; jest.mock('simple-git/promise'); const simpleGit: any = _simpleGit; @@ -31,7 +31,7 @@ describe('datasource/git-refs', () => { simpleGit.mockReturnValue({ listRemote() { return Promise.resolve( - 'commithash1\trefs/tags/0.0.1\ncommithash2\trefs/tags/v0.0.2\ncommithash3\trefs/tags/v0.0.2^{}\ncommithash4\trefs/heads/v0.0.3\ncommithash5\trefs/tags/v0.0.3\n' + 'commithash0\tHEAD\ncommithash1\trefs/tags/0.0.1\ncommithash2\trefs/tags/v0.0.2\ncommithash3\trefs/tags/v0.0.2^{}\ncommithash4\trefs/heads/v0.0.3\ncommithash5\trefs/tags/v0.0.3\n' ); }, }); @@ -44,4 +44,48 @@ describe('datasource/git-refs', () => { expect(result).toEqual(['0.0.1', 'v0.0.2', 'v0.0.3']); }); }); + describe('getDigest()', () => { + it('returns null if not found', async () => { + simpleGit.mockReturnValue({ + listRemote() { + return Promise.resolve( + 'commithash0\tHEAD\ncommithash1\trefs/tags/0.0.1\ncommithash2\trefs/tags/v0.0.2\ncommithash3\trefs/tags/v0.0.2^{}\ncommithash4\trefs/heads/v0.0.3\ncommithash5\trefs/tags/v0.0.3\n' + ); + }, + }); + const digest = await getDigest( + { lookupName: 'a tag to look up' }, + 'v1.0.2' + ); + expect(digest).toBeNull(); + }); + it('returns digest for tag', async () => { + simpleGit.mockReturnValue({ + listRemote() { + return Promise.resolve( + 'commithash0\tHEAD\ncommithash1\trefs/tags/0.0.1\ncommithash2\trefs/tags/v0.0.2\ncommithash3\trefs/tags/v0.0.2^{}\ncommithash4\trefs/heads/v0.0.3\ncommithash5\trefs/tags/v0.0.3\n' + ); + }, + }); + const digest = await getDigest( + { lookupName: 'a tag to look up' }, + 'v0.0.2' + ); + expect(digest).toEqual('commithash2'); + }); + it('returns digest for HEAD', async () => { + simpleGit.mockReturnValue({ + listRemote() { + return Promise.resolve( + 'commithash0\tHEAD\ncommithash1\trefs/tags/0.0.1\ncommithash2\trefs/tags/v0.0.2\ncommithash3\trefs/tags/v0.0.2^{}\ncommithash4\trefs/heads/v0.0.3\ncommithash5\trefs/tags/v0.0.3\n' + ); + }, + }); + const digest = await getDigest( + { lookupName: 'another tag to look up' }, + undefined + ); + expect(digest).toEqual('commithash0'); + }); + }); }); diff --git a/lib/datasource/git-refs/index.ts b/lib/datasource/git-refs/index.ts index 6144276a80e9345753e2e41ccb4f846670d3de64..967db613b2354179e8028c20ffd930302142ddbe 100644 --- a/lib/datasource/git-refs/index.ts +++ b/lib/datasource/git-refs/index.ts @@ -1,7 +1,7 @@ import simpleGit from 'simple-git/promise'; import { logger } from '../../logger'; import * as semver from '../../versioning/semver'; -import { GetReleasesConfig, ReleaseResult } from '../common'; +import { DigestConfig, GetReleasesConfig, ReleaseResult } from '../common'; export const id = 'git-refs'; @@ -45,21 +45,31 @@ export async function getRawRefs({ } const refMatch = /(?<hash>.*?)\s+refs\/(?<type>.*?)\/(?<value>.*)/; + const headMatch = /(?<hash>.*?)\s+HEAD/; const refs = lsRemote .trim() .split('\n') .map((line) => line.trim()) .map((line) => { - const match = refMatch.exec(line); - if (!match) { - return null; + let match = refMatch.exec(line); + if (match) { + return { + type: match.groups.type, + value: match.groups.value, + hash: match.groups.hash, + }; } - return { - type: match.groups.type, - value: match.groups.value, - hash: match.groups.hash, - }; + match = headMatch.exec(line); + if (match) { + return { + type: '', + value: 'HEAD', + hash: match.groups.hash, + }; + } + // istanbul ignore next + return null; }) .filter(Boolean); @@ -101,3 +111,16 @@ export async function getReleases({ } return null; } + +export async function getDigest( + { lookupName }: Partial<DigestConfig>, + newValue?: string +): Promise<string | null> { + const rawRefs: RawRefs[] = await getRawRefs({ lookupName }); + const findValue = newValue || 'HEAD'; + const ref = rawRefs.find((rawRef) => rawRef.value === findValue); + if (ref) { + return ref.hash; + } + return null; +} diff --git a/lib/datasource/git-tags/index.spec.ts b/lib/datasource/git-tags/index.spec.ts index f059403e0bd0bcda7b72a2969f9661adfb1f0a74..ef1fa94fa799110d1cc1b142be8c3ec502303e99 100644 --- a/lib/datasource/git-tags/index.spec.ts +++ b/lib/datasource/git-tags/index.spec.ts @@ -1,5 +1,5 @@ import _simpleGit from 'simple-git/promise'; -import { getReleases } from '.'; +import { getDigest, getReleases } from '.'; jest.mock('simple-git/promise'); const simpleGit: any = _simpleGit; @@ -42,4 +42,48 @@ describe('datasource/git-tags', () => { expect(versions).toMatchSnapshot(); }); }); + describe('getDigest()', () => { + it('returns null if not found', async () => { + simpleGit.mockReturnValue({ + listRemote() { + return Promise.resolve( + 'commithash0\tHEAD\ncommithash1\trefs/tags/0.0.1\ncommithash2\trefs/tags/v0.0.2\ncommithash3\trefs/tags/v0.0.2^{}\ncommithash4\trefs/heads/v0.0.3\ncommithash5\trefs/tags/v0.0.3\n' + ); + }, + }); + const digest = await getDigest( + { lookupName: 'a tag to look up' }, + 'v1.0.2' + ); + expect(digest).toBeNull(); + }); + it('returns digest for tag', async () => { + simpleGit.mockReturnValue({ + listRemote() { + return Promise.resolve( + 'commithash0\tHEAD\ncommithash1\trefs/tags/0.0.1\ncommithash2\trefs/tags/v0.0.2\ncommithash3\trefs/tags/v0.0.2^{}\ncommithash4\trefs/heads/v0.0.3\ncommithash5\trefs/tags/v0.0.3\n' + ); + }, + }); + const digest = await getDigest( + { lookupName: 'a tag to look up' }, + 'v0.0.2' + ); + expect(digest).toEqual('commithash2'); + }); + it('returns digest for HEAD', async () => { + simpleGit.mockReturnValue({ + listRemote() { + return Promise.resolve( + 'commithash0\tHEAD\ncommithash1\trefs/tags/0.0.1\ncommithash2\trefs/tags/v0.0.2\ncommithash3\trefs/tags/v0.0.2^{}\ncommithash4\trefs/heads/v0.0.3\ncommithash5\trefs/tags/v0.0.3\n' + ); + }, + }); + const digest = await getDigest( + { lookupName: 'another tag to look up' }, + undefined + ); + expect(digest).toEqual('commithash0'); + }); + }); }); diff --git a/lib/datasource/git-tags/index.ts b/lib/datasource/git-tags/index.ts index 6014caec6416ebd2730ac502e385cc3e7f61eaf2..892fcc465ba027e8bec722717a219028f87e3837 100644 --- a/lib/datasource/git-tags/index.ts +++ b/lib/datasource/git-tags/index.ts @@ -1,5 +1,5 @@ import * as semver from '../../versioning/semver'; -import { GetReleasesConfig, ReleaseResult } from '../common'; +import { DigestConfig, GetReleasesConfig, ReleaseResult } from '../common'; import * as gitRefs from '../git-refs'; export const id = 'git-tags'; @@ -30,3 +30,16 @@ export async function getReleases({ return result; } + +export async function getDigest( + { lookupName }: Partial<DigestConfig>, + newValue?: string +): Promise<string | null> { + const rawRefs: gitRefs.RawRefs[] = await gitRefs.getRawRefs({ lookupName }); + const findValue = newValue || 'HEAD'; + const ref = rawRefs.find((rawRef) => rawRef.value === findValue); + if (ref) { + return ref.hash; + } + return null; +}