From c9b689d67b36bae30b37f4c108345857544f541c Mon Sep 17 00:00:00 2001 From: Felix Yan Date: Thu, 24 Sep 2020 04:25:08 +0800 Subject: [PATCH 1/4] Add a git source It's a thin wrapper around the cmd source, and reuses its get_cmd function. --- docs/usage.rst | 13 +++++++++++++ nvchecker_source/git.py | 15 +++++++++++++++ tests/test_git.py | 11 +++++++++++ 3 files changed, 39 insertions(+) create mode 100644 nvchecker_source/git.py create mode 100644 tests/test_git.py diff --git a/docs/usage.rst b/docs/usage.rst index d0fdf86..ba3599e 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -641,6 +641,19 @@ strip_release Note that either pkg or srcpkg needs to be specified (but not both) or the item name will be used as pkg. +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. + +url + URL of the Git repository. + +This source returns tags and supports :ref:`list options`. + Manually updating ~~~~~~~~~~~~~~~~~ :: diff --git a/nvchecker_source/git.py b/nvchecker_source/git.py new file mode 100644 index 0000000..5571768 --- /dev/null +++ b/nvchecker_source/git.py @@ -0,0 +1,15 @@ +# MIT licensed +# Copyright (c) 2020 Felix Yan , et al. + +import re +from nvchecker_source.cmd import run_cmd + +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) + regex = "(?<=refs/tags/).*$" + + return re.findall(regex, data) diff --git a/tests/test_git.py b/tests/test_git.py new file mode 100644 index 0000000..03a9c5a --- /dev/null +++ b/tests/test_git.py @@ -0,0 +1,11 @@ +# MIT licensed +# Copyright (c) 2020 Felix Yan , et al. + +import pytest +pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net] + +async def test_git(get_version): + assert await get_version("example", { + "source": "git", + "git": "https://gitlab.com/gitlab-org/gitlab-test.git", + }) == "v1.1.1" From d7624defd7d05e721aeb0ccd074c7172c51295bf Mon Sep 17 00:00:00 2001 From: Felix Yan Date: Thu, 24 Sep 2020 04:51:28 +0800 Subject: [PATCH 2/4] Fix mypy and regex flag --- nvchecker_source/git.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nvchecker_source/git.py b/nvchecker_source/git.py index 5571768..14459a7 100644 --- a/nvchecker_source/git.py +++ b/nvchecker_source/git.py @@ -2,7 +2,7 @@ # Copyright (c) 2020 Felix Yan , et al. import re -from nvchecker_source.cmd import run_cmd +from .cmd import run_cmd # type: ignore async def get_version( name, conf, *, cache, keymanager=None @@ -12,4 +12,4 @@ async def get_version( data = await cache.get(cmd, run_cmd) regex = "(?<=refs/tags/).*$" - return re.findall(regex, data) + return re.findall(regex, data, re.MULTILINE) From 7213d84056642c62976c5a54bf8b6ad706b192ee Mon Sep 17 00:00:00 2001 From: Felix Yan Date: Thu, 24 Sep 2020 05:45:09 +0800 Subject: [PATCH 3/4] Avoid using regex at all --- nvchecker_source/git.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/nvchecker_source/git.py b/nvchecker_source/git.py index 14459a7..89e0b6d 100644 --- a/nvchecker_source/git.py +++ b/nvchecker_source/git.py @@ -1,7 +1,6 @@ # MIT licensed # Copyright (c) 2020 Felix Yan , et al. -import re from .cmd import run_cmd # type: ignore async def get_version( @@ -10,6 +9,5 @@ async def get_version( git = conf['git'] cmd = f"git ls-remote -t --refs {git}" data = await cache.get(cmd, run_cmd) - regex = "(?<=refs/tags/).*$" - - return re.findall(regex, data, re.MULTILINE) + versions = list(map(lambda line: line.split("refs/tags/")[1], data.split("\n"))) + return versions From 3ffb34257acdd58ec8929bf7ec7d5bd2567be334 Mon Sep 17 00:00:00 2001 From: Felix Yan Date: Thu, 24 Sep 2020 16:13:30 +0800 Subject: [PATCH 4/4] Use list comprehension instead of map lambda --- nvchecker_source/git.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nvchecker_source/git.py b/nvchecker_source/git.py index 89e0b6d..2f70104 100644 --- a/nvchecker_source/git.py +++ b/nvchecker_source/git.py @@ -9,5 +9,5 @@ async def get_version( git = conf['git'] cmd = f"git ls-remote -t --refs {git}" data = await cache.get(cmd, run_cmd) - versions = list(map(lambda line: line.split("refs/tags/")[1], data.split("\n"))) + versions = [line.split("refs/tags/")[1] for line in data.splitlines()] return versions