From d360a6d92d744762f9f13555ed03726b6d4ba937 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Wed, 16 Nov 2016 14:12:18 +0100 Subject: DebianGitRepository: simplify version mangling '%' is not valid in a Debian version number and we only want single character replacements for now. --- docs/chapters/import.sgml | 8 +++++--- gbp/deb/git.py | 19 +++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/docs/chapters/import.sgml b/docs/chapters/import.sgml index 89d15b83..c42279c9 100644 --- a/docs/chapters/import.sgml +++ b/docs/chapters/import.sgml @@ -274,8 +274,9 @@ upstream-tag = v%(version)s numbers by dots but rather by underscore(_), hyphen(-) or anything else. In - order to cope with that you can use version mangling via - substitution. The substitution works as follows: + order to cope with that you can use version mangling of these + characters via substitution. The substitution works as + follows: [buildpackage] @@ -292,9 +293,10 @@ upstream-tag = v%(version%.%_)s &gbp-buildpackage;. - If you need to use the % character in either of the substitution strings, + If you want the substitution to be the % character you have to escape it. E.g. %(version%-%\%)s will replace - with %, transforming 1-A.B.C to 1%A.B.C. + Only a single replacement is supported and it can only replace a single character. If you're using &pristine-tar;, you can make &gbp-buildpackage; commit the generated tarball back to the diff --git a/gbp/deb/git.py b/gbp/deb/git.py index 80fb23b7..fea919d9 100644 --- a/gbp/deb/git.py +++ b/gbp/deb/git.py @@ -27,6 +27,11 @@ import gbp.log class DebianGitRepository(GitRepository): """A git repository that holds the source of a Debian package""" + version_mangle_re = (r'%\(version' + '%(?P[^%])' + '%(?P([^%]|\\%))+' + '\)s') + def __init__(self, path): super(DebianGitRepository, self).__init__(path) self.pristine_tar = DebianPristineTar(self) @@ -132,8 +137,8 @@ class DebianGitRepository(GitRepository): version = version.replace('~', '.') return format % dict(version=version) - @staticmethod - def version_to_tag(format, version): + @classmethod + def version_to_tag(cls, format, version): """Generate a tag from a given format and a version %(version)s provides a clean version that works as a git tag. @@ -144,7 +149,7 @@ class DebianGitRepository(GitRepository): %(version%A%B)s provides %(version)s with string 'A' replaced by 'B'. This way, simple version mangling is possible via substitution. - Inside either substition string, '%' needs to be escaped. See the + Inside the substition string, '%' needs to be escaped. See the examples below. >>> DebianGitRepository.version_to_tag("debian/%(version)s", "0:0~0") @@ -155,13 +160,11 @@ class DebianGitRepository(GitRepository): 'v1_2_3' >>> DebianGitRepository.version_to_tag("%(version%-%\%)s", "0-1.2.3") '0%1.2.3' - >>> DebianGitRepository.version_to_tag("%(version%\%%.)s", "0%1%2%3") - '0.1.2.3' """ - r = re.search(r"%\(version%([^%]+|.*\\%.*)%([^%]+|.*\\%.*)\)s", format) + r = re.search(cls.version_mangle_re, format) if r: - format = re.sub(r"%\(version%([^%]+|.*\\%.*)%([^%]|.*\\%.*)+\)s", "%(version)s", format) - version = version.replace(r.group(1).replace('\%', '%'), r.group(2).replace('\%', '%')) + format = re.sub(cls.version_mangle_re, "%(version)s", format) + version = version.replace(r.group('M'), r.group('R').replace('\%', '%')) return format_str(format, dict(version=DebianGitRepository._sanitize_version(version), hversion=DebianGitRepository._sanitize_version(version).replace('.', '-'))) -- cgit v1.2.3