diff --git a/docs/usage.rst b/docs/usage.rst index 5ae2b39..56c88e2 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -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 ~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/nvchecker_source/git.py b/nvchecker_source/git.py index 2f70104..87b2118 100644 --- a/nvchecker_source/git.py +++ b/nvchecker_source/git.py @@ -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 diff --git a/tests/test_git.py b/tests/test_git.py index 03a9c5a..c570a02 100644 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -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"