Fix tag match with GitPython

This commit is contained in:
linsui 2022-08-09 22:37:44 +08:00 committed by Jochen Sprickerhof
parent 737ad53d3c
commit 516a0c2ce8
2 changed files with 30 additions and 46 deletions

View File

@ -1238,34 +1238,15 @@ class vcs_git(vcs):
p = FDroidPopen(['git', 'tag'], cwd=self.local, output=False)
return p.output.splitlines()
tag_format = re.compile(r'tag: ([^) ]*)')
def latesttags(self):
"""Return a list of latest tags.
The definition is a little blurry here, Android does not care for the
version name of an app as normally used as the tag name so versions do
not need to follow strverscmp() or similar. Also they can be rather
arbitrary so git tag --sort=-version:refname does not work. On the other side
sorting them by creation date, i.e. git tag --sort=-authordate does not
work either as there are a lot of repos where older tags were created
later.
So git log preserves the graph order and only sorts by date afterwards.
This results in tags of beta versions being sorted earlier then the
latest tag as long as they are part of the graph below the latest tag
or are created earlier.
"""
"""Return a list of latest tags."""
self.checkrepo()
p = FDroidPopen(['git', 'log', '--tags',
'--simplify-by-decoration', '--pretty=format:%d'],
cwd=self.local, output=False)
tags = []
for line in p.output.splitlines():
for entry in line.split(', '):
for tag in self.tag_format.findall(entry):
tags.append(tag)
return tags
# TODO: Python3.6: Should accept path-like
return [tag.name for tag in sorted(
git.Repo(self.local).tags,
key=lambda t: t.commit.committed_date,
reverse=True
)]
def getref(self, revname='HEAD'):
self.checkrepo()

View File

@ -2357,26 +2357,29 @@ class CommonTest(unittest.TestCase):
)
def test_vcs_git_latesttags(self):
vcs = fdroidserver.common.vcs_git(None, None)
popenmock = mock.Mock()
popenmock.output = """
(HEAD, tag: 8.9.5, origin/master, origin/HEAD, master)
(tag: 8.9.4)
(tag: 8.9.3, tag: 8,9,3)
(tag: 8.9.3b)
(tag: awesome_release)
(origin/feature/cast)
(tag: 8.6.3)
"""
with mock.patch(
'fdroidserver.common.FDroidPopen', lambda a, **b: popenmock
) as _ignored, mock.patch(
'fdroidserver.common.vcs_git.checkrepo'
) as _ignored:
_ignored # silence the linters
tags = vcs.latesttags()
self.assertEqual(tags, ['8.9.5', '8.9.4', '8.9.3', '8,9,3',
'8.9.3b', 'awesome_release', '8.6.3'])
tags = [
"1.1.1",
"2.2.2",
"v3.0",
"0.0.4",
"0.5.0-beta",
"666(6)",
"seven",
]
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
# TODO: Python3.6: Should accept path-like
repo = git.Repo.init(str(Path.cwd()))
f = Path("test")
date = 10**9
for tag in tags:
date += 1
f.write_text(tag)
repo.index.add([str(f)])
repo.index.commit(tag, commit_date=str(date) + " +0000")
repo.create_tag(tag)
vcs = fdroidserver.common.vcs_git(None, Path.cwd())
self.assertEqual(vcs.latesttags(), tags[::-1])
def test_get_release_filename(self):
app = fdroidserver.metadata.App()