Package: git-buildpackage
Followup-For: Bug #1010751

Hello.
The attached commit implements your suggestions.
>From 3f8debeeeffbf57e77234848317b38d12b2d3363 Mon Sep 17 00:00:00 2001
From: Nicolas Boulenguez <nico...@debian.org>
Date: Sun, 8 May 2022 16:51:26 +0200
Subject: [PATCH] clone: handle -b optional branch specification in VCS-Git

---
 gbp/scripts/clone.py       | 35 ++++++++++++++++++++++++-----------
 tests/29_test_gbp_clone.py | 18 ++++++++++++++++--
 2 files changed, 40 insertions(+), 13 deletions(-)

diff --git a/gbp/scripts/clone.py b/gbp/scripts/clone.py
index 7e02f0e2..4825c572 100755
--- a/gbp/scripts/clone.py
+++ b/gbp/scripts/clone.py
@@ -47,10 +47,15 @@ def apt_showsrc(pkg):
 
 
 def vcs_git_url(pkg):
+    """
+    (url,  None)   most of the time
+    (url,  branch) when the VCS-Git contains -b
+    (None, None)   when the VCS-Git field is missing
+    """
     repos = {}
 
     out = apt_showsrc(pkg)
-    vcs_re = re.compile(r'(x-)?vcs-git:\s*(?P<repo>[^ ]+)$', re.I)
+    vcs_re = re.compile(r'(?:x-)?vcs-git:\s*([^ ]+)(?:\s+-b\s+([^ ]+))?$', re.I)
     version_re = re.compile(r'Version:\s*(?P<version>.*)$', re.I)
     end_re = re.compile(r'\s*$')
 
@@ -58,7 +63,7 @@ def vcs_git_url(pkg):
     for line in out.split('\n'):
         m = vcs_re.match(line)
         if m:
-            repo = m.group('repo')
+            repo = m.groups()
             continue
         m = version_re.match(line)
         if m:
@@ -72,7 +77,7 @@ def vcs_git_url(pkg):
 
     if not repos:
         gbp.log.err("Can't find any vcs-git URL for '%s'" % pkg)
-        return None
+        return None, None
 
     s = sorted(repos, key=cmp_to_key(DpkgCompareVersions()))
     return repos[s[-1]]
@@ -80,27 +85,31 @@ def vcs_git_url(pkg):
 
 def repo_to_url(repo):
     """
+    (url,  None)   most of the time
+    (url,  branch) when VCS-Git is required and contains a -b option
+    (None, None)   when VCS-Git is required but missing.
+
     >>> repo_to_url("https://foo.example.com";)
-    'https://foo.example.com'
+    ('https://foo.example.com', None)
     >>> repo_to_url("salsa:agx/git-buildpackage")
-    'https://salsa.debian.org/agx/git-buildpackage.git'
+    ('https://salsa.debian.org/agx/git-buildpackage.git', None)
     >>> repo_to_url("github:agx/git-buildpackage")
-    'https://github.com/agx/git-buildpackage.git'
+    ('https://github.com/agx/git-buildpackage.git', None)
     """
     parts = repo.split(":", 1)
     if len(parts) != 2:
-        return repo
+        return repo, None
     else:
         proto, path = parts
 
     if proto == 'salsa':
-        return 'https://salsa.debian.org/%s.git' % path
+        return 'https://salsa.debian.org/%s.git' % path, None
     if proto == 'github':
-        return 'https://github.com/%s.git' % path
+        return 'https://github.com/%s.git' % path, None
     elif proto in ['vcsgit', 'vcs-git']:
         return vcs_git_url(path)
     else:
-        return repo
+        return repo, None
 
 
 def add_upstream_vcs(repo):
@@ -189,7 +198,7 @@ def main(argv):
         return 1
     else:
         remote_repo = args[1]
-        source = repo_to_url(remote_repo) if options.aliases else remote_repo
+        source, vcs_git_branch = repo_to_url(remote_repo) if options.aliases else remote_repo, None
         if not source:
             return 1
 
@@ -212,6 +221,10 @@ def main(argv):
         postclone = options.postclone
         (options, args) = parse_args(argv)
 
+        if vcs_git_branch and vcs_git_branch != options.debian_branch:
+            gbp.log.warn(f'VCS-Git: -b {vcs_git_branch} overrides --debian-branch={options.debian_branch}')
+            options.debian_branch = vcs_git_branch
+
         # Track all branches:
         if options.all:
             remotes = repo.get_remote_branches()
diff --git a/tests/29_test_gbp_clone.py b/tests/29_test_gbp_clone.py
index f1ac3925..b1930612 100644
--- a/tests/29_test_gbp_clone.py
+++ b/tests/29_test_gbp_clone.py
@@ -15,7 +15,7 @@ Vcs-Git: git://honk.sigxcpu.org/git/git-buildpackage.git
 
 Version: 0.8.14
 Standards-Version: 3.9.8
-Vcs-Git: https://git.sigxcpu.org/cgit/git-buildpackage/ -b foo
+Vcs-Git: https://git.sigxcpu.org/cgit/git-buildpackage/ unexpected_info
 
 Version: 0.8.12.2
 Standards-Version: 3.9.8
@@ -31,4 +31,18 @@ Vcs-Git: git://honk.sigxcpu.org/git/git-buildpackage.git
     @patch('gbp.scripts.clone.apt_showsrc', return_value=show_src)
     def test_vcs_git_url(self, patch):
         self.assertEqual(vcs_git_url('git-buildpackage'),
-                         'https://git.sigxcpu.org/cgit/git-buildpackage/')
+                         ('https://git.sigxcpu.org/cgit/git-buildpackage/', None))
+
+    @skip_without_cmd('dpkg')
+    @patch('gbp.scripts.clone.apt_showsrc', return_value="""
+Version: 0.7.6-4
+Vcs-Git: https://git.codelabs.ch/git/pcscada.git -b debian
+""")
+    def test_vcs_git_url_branch(self, patch):
+        self.assertEqual(vcs_git_url('pcscada'),
+                         ('https://git.codelabs.ch/git/pcscada.git', 'debian'))
+
+    @skip_without_cmd('dpkg')
+    @patch('gbp.scripts.clone.apt_showsrc', return_value='')
+    def test_vcs_git_url_missing(self, patch):
+        self.assertEqual(vcs_git_url('ignored'), (None, None))
-- 
2.30.2

Reply via email to