From: Markus Lehtonen Date: Fri, 6 Sep 2013 11:23:03 +0000 (+0300) Subject: common/pq: support more flexible pq-branch naming X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0948e1af309c45c5a2044b2e4ed8017de7c4b86d;p=tools%2Fgit-buildpackage.git common/pq: support more flexible pq-branch naming Now any string fields (e.g. '%(version)s') can be used in the pq-branch format strings - in addition to the '%(branch)s' supported before. Signed-off-by: Markus Lehtonen --- diff --git a/gbp/scripts/common/pq.py b/gbp/scripts/common/pq.py index ab72f6f9..fc69bb57 100644 --- a/gbp/scripts/common/pq.py +++ b/gbp/scripts/common/pq.py @@ -37,6 +37,33 @@ import gbp.log DEFAULT_PQ_BRANCH_NAME = "patch-queue/%(branch)s" +def pq_branch_match(branch, pq_fmt_str): + """ + Match branch name with pq branch name pattern + + >>> pq_branch_match('patch-queue/foo', 'patch-queue/%(branch)s').groupdict() + {'branch': 'foo'} + >>> pq_branch_match('pq/foo/bar', 'pq/%(branch)s/baz') + >>> pq_branch_match('pq/foo/bar', 'pq/%(branch)s/bar').groupdict() + {'branch': 'foo'} + >>> pq_branch_match('foo/bar/1.0/pq', + ... 'foo/%(branch)s/%(ver)s/pq').groupdict() + {'ver': '1.0', 'branch': 'bar'} + >>> pq_branch_match('foo/bar/1.0/pq', + ... 'foo/%(b)s/%(ver)s/pq').groupdict() # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + GbpError: Invalid pq-branch, name format must contain %(branch)s... + + """ + pq_re_str = '^%s$' % re.sub('%\(([a-z_\-]+)\)s', r'(?P<\1>\S+)', pq_fmt_str) + pq_re = re.compile(pq_re_str) + if not 'branch' in pq_re.groupindex: + raise GbpError("Invalid pq-branch, name format must contain %(branch)s " + "keyword for identifying the base branch") + return pq_re.match(branch) + + def is_pq_branch(branch, options): """ is branch a patch-queue branch? @@ -53,25 +80,29 @@ def is_pq_branch(branch, options): >>> is_pq_branch("bar/foo/development", opts) True >>> opts.pq_branch = "development" - >>> is_pq_branch("development", opts) - True + >>> is_pq_branch("development", opts) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + GbpError: Invalid pq-branch, name format must contain %(branch)s... >>> opts.pq_branch = "my/%(branch)s/pq" >>> is_pq_branch("my/foo/pqb", opts) False >>> is_pq_branch("my/foo/pq", opts) True + >>> opts.pq_branch = "my/%(branch)s/%(version)s" + >>> is_pq_branch("my/foo", opts) + False + >>> is_pq_branch("my/foo/1.0", opts) + True """ - pq_format_str = DEFAULT_PQ_BRANCH_NAME - if hasattr(options, 'pq_branch'): - pq_format_str = options.pq_branch - - pq_re = re.compile(r'^%s$' % (pq_format_str % dict(branch="(?P\S+)"))) - if pq_re.match(branch): + pq_format_str = (options.pq_branch if hasattr(options, 'pq_branch') + else DEFAULT_PQ_BRANCH_NAME) + if pq_branch_match(branch, pq_format_str): return True return False -def pq_branch_name(branch, options): +def pq_branch_name(branch, options, extra_keys=None): """ get the patch queue branch corresponding to branch @@ -84,26 +115,29 @@ def pq_branch_name(branch, options): >>> pq_branch_name("foo", opts) 'foo/development' >>> opts.pq_branch = "development" - >>> pq_branch_name("foo", opts) - 'development' + >>> pq_branch_name("foo", opts) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + GbpError: Invalid pq-branch, name format must contain %(branch)s... + >>> opts.pq_branch = "pq/%(branch)s/%(ver)s" + >>> pq_branch_name("foo", opts, {'ver': '1.0'}) + 'pq/foo/1.0' """ - pq_format_str = DEFAULT_PQ_BRANCH_NAME - if hasattr(options, 'pq_branch'): - pq_format_str = options.pq_branch - + pq_format_str = (options.pq_branch if hasattr(options, 'pq_branch') + else DEFAULT_PQ_BRANCH_NAME) + format_fields = {'branch': branch} + if extra_keys: + format_fields.update(extra_keys) if not is_pq_branch(branch, options): - return pq_format_str % dict(branch=branch) + return pq_format_str % format_fields def pq_branch_base(pq_branch, options): """ Get the branch corresponding to the given patch queue branch. - Returns the packaging/debian branch if pq format string doesn't contain - '%(branch)s' key. >>> from optparse import OptionParser >>> (opts, args) = OptionParser().parse_args([]) - >>> opts.packaging_branch = "packaging" >>> pq_branch_base("patch-queue/master", opts) 'master' >>> pq_branch_base("foo", opts) @@ -113,20 +147,16 @@ def pq_branch_base(pq_branch, options): >>> pq_branch_base("my/foo/development", opts) 'foo' >>> opts.pq_branch = "development" - >>> pq_branch_base("foo/development", opts) - >>> pq_branch_base("development", opts) - 'packaging' + >>> pq_branch_base("development", opts) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + GbpError: Invalid pq-branch, name format must contain %(branch)s... """ - pq_format_str = DEFAULT_PQ_BRANCH_NAME - if hasattr(options, 'pq_branch'): - pq_format_str = options.pq_branch - - pq_re = re.compile(r'^%s$' % (pq_format_str % dict(branch="(?P\S+)"))) - m = pq_re.match(pq_branch) + pq_format_str = (options.pq_branch if hasattr(options, 'pq_branch') + else DEFAULT_PQ_BRANCH_NAME) + m = pq_branch_match(pq_branch, pq_format_str) if m: - if 'base' in m.groupdict(): - return m.group('base') - return options.packaging_branch + return m.group('branch') def parse_gbp_commands(info, cmd_tag, noarg_cmds, arg_cmds, filter_cmds=None): @@ -331,7 +361,7 @@ def get_maintainer_from_control(repo): return GitModifier() -def switch_to_pq_branch(repo, branch, options): +def switch_to_pq_branch(repo, branch, options, name_keys=None): """ Switch to patch-queue branch if not already there, create it if it doesn't exist yet @@ -339,7 +369,7 @@ def switch_to_pq_branch(repo, branch, options): if is_pq_branch(branch, options): return - pq_branch = pq_branch_name(branch, options) + pq_branch = pq_branch_name(branch, options, name_keys) if not repo.has_branch(pq_branch): try: repo.create_branch(pq_branch) @@ -383,12 +413,12 @@ def apply_and_commit_patch(repo, patch, fallback_author, topic=None): repo.update_ref('HEAD', commit, msg="gbp-pq import %s" % patch.path) -def drop_pq(repo, branch, options): +def drop_pq(repo, branch, options, name_keys=None): if is_pq_branch(branch, options): gbp.log.err("On a patch-queue branch, can't drop it.") raise GbpError else: - pq_branch = pq_branch_name(branch, options) + pq_branch = pq_branch_name(branch, options, name_keys) if repo.has_branch(pq_branch): repo.delete_branch(pq_branch) diff --git a/tests/13_test_gbp_pq.py b/tests/13_test_gbp_pq.py index dedcb9e3..dfcf87a6 100644 --- a/tests/13_test_gbp_pq.py +++ b/tests/13_test_gbp_pq.py @@ -147,7 +147,7 @@ class TestExport(testutils.DebianGitTestRepo): repo = self.repo start = repo.get_branch() pq_branch = os.path.join('patch-queue', start) - switch_pq(repo, start, TestExport.Options) + pq.switch_pq(repo, start, TestExport.Options) self.assertEqual(repo.get_branch(), pq_branch) export_patches(repo, pq_branch, TestExport.Options) self.assertEqual(repo.get_branch(), start)