git source: support fetching commit hash on a branch

This commit is contained in:
lilydjwg 2020-12-24 22:00:26 +08:00
parent a0c32ce5f0
commit 491a71add7
3 changed files with 39 additions and 6 deletions

View File

@ -666,12 +666,18 @@ Check Git repository
source = "git"
This enables you to check tags of an arbitrary git repository, also useful for scenarios like a github project having too many tags.
This enables you to check tags or branch commits of an arbitrary git repository, also useful for scenarios like a github project having too many tags.
git
URL of the Git repository.
This source returns tags and supports :ref:`list options`.
use_commit
Return a commit hash instead of tags.
branch
When ``use_commit`` is true, return the commit on the specified branch instead of the default one.
When this source returns tags (``use_commit`` is not true) it supports :ref:`list options`.
Check container registry
~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -7,7 +7,19 @@ async def get_version(
name, conf, *, cache, keymanager=None
):
git = conf['git']
cmd = f"git ls-remote -t --refs {git}"
data = await cache.get(cmd, run_cmd)
versions = [line.split("refs/tags/")[1] for line in data.splitlines()]
return versions
use_commit = conf.get('use_commit', False)
if use_commit:
ref = conf.get('branch')
if ref is None:
ref = 'HEAD'
else:
ref = 'refs/heads/' + ref
cmd = f"git ls-remote {git} {ref}"
data = await cache.get(cmd, run_cmd)
return data.split(None, 1)[0]
else:
cmd = f"git ls-remote --tags --refs {git}"
data = await cache.get(cmd, run_cmd)
versions = [line.split("refs/tags/")[1] for line in data.splitlines()]
return versions

View File

@ -9,3 +9,18 @@ async def test_git(get_version):
"source": "git",
"git": "https://gitlab.com/gitlab-org/gitlab-test.git",
}) == "v1.1.1"
async def test_git_commit(get_version):
assert await get_version("example", {
"source": "git",
"git": "https://gitlab.com/gitlab-org/gitlab-test.git",
"use_commit": True,
}) == "ddd0f15ae83993f5cb66a927a28673882e99100b"
async def test_git_commit_branch(get_version):
assert await get_version("example", {
"source": "git",
"git": "https://gitlab.com/gitlab-org/gitlab-test.git",
"use_commit": True,
"branch": "with-executables",
}) == "6b8dc4a827797aa025ff6b8f425e583858a10d4f"