clone: Allow to add automatically add upstream vcs
authorGuido Günther <agx@sigxcpu.org>
Tue, 2 Aug 2022 07:45:30 +0000 (09:45 +0200)
committerGuido Günther <agx@sigxcpu.org>
Tue, 2 Aug 2022 08:24:58 +0000 (10:24 +0200)
While "gbp import-orig" supports "--upstream-vcs" since some time to
link to the upstream git history however setup of the upstream remoet
repo so far was manual.

To make this more automatic add "--add-upstream-vcs" to "gbp clone"
(defaulting to False) and set up the upstreamvcs remote in case there's
sufficient information in debian/upstream/metadata. Once added fetch
from that remote initially.

debian/control
docs/manpages/gbp-clone.xml
gbp/config.py
gbp/scripts/clone.py
tests/component/deb/test_clone.py

index 255dc53400bcfc52662a72e7e9d004969ebfcfed..c5de80dcf91d778003fa076b71d0682c8d476418 100644 (file)
@@ -23,6 +23,7 @@ Build-Depends:
  python3-pkg-resources,
  python3-rpm,
  python3-setuptools,
+ python3-yaml,
  xsltproc,
 # For the testsuite
  bzip2 <!nocheck>,
@@ -53,6 +54,7 @@ Depends: ${python3:Depends},
  man-db,
  python3-dateutil,
  python3-pkg-resources,
+ python3-yaml,
  sensible-utils,
 Recommends: pristine-tar (>= 1.41),
  cowbuilder | pbuilder | sbuild,
index 484304dbef2751404681c20e08ecef24271130b8..042b3d219b217767e9a3392ef7995fc46db11076 100644 (file)
@@ -34,6 +34,7 @@
       <arg><option>--repo-user=</option><option>[GIT|DEBIAN]</option></arg>
       <arg><option>--repo-email=</option><option>[GIT|DEBIAN]</option></arg>
       <arg><option>--[no-]aliases</option></arg>
+      <arg><option>--[no-]add-upstream-vcs</option></arg>
       <arg choice="plain"><replaceable>repository</replaceable></arg>
       <arg><replaceable>directory</replaceable></arg>
     </cmdsynopsis>
           </para>
         </listitem>
       </varlistentry>
+      <varlistentry>
+        <term><option>--[no-]add-upstream-vcs</option>
+        </term>
+        <listitem>
+          <para>
+           Whether to add the upstream git repository as additional remote. The repositor url is read from
+            <filename>debian/upstream/meta</filename>.
+          </para>
+        </listitem>
+      </varlistentry>
       <varlistentry>
        <term><replaceable>repository</replaceable></term>
        <listitem>
index e5a0abc78efc41fdb22b93c4c92ad178a4872e51..0a5a35e4fd785a6da26cd1226d9bbb2c3e1e9ee0 100644 (file)
@@ -108,6 +108,7 @@ class GbpOptionParser(OptionParser):
     @type def_config_files: dict (type, path)
     """
     defaults = {'abbrev': 7,
+                'add-upstream-vcs': 'False',
                 'aliases': 'True',
                 'allow-unauthenticated': 'False',
                 'arch': '',
@@ -201,6 +202,9 @@ class GbpOptionParser(OptionParser):
                 'urgency': 'medium',
                 }
     help = {
+        'add-upstream-vcs':
+            "Whether to add the upstream vcs as additional remote "
+            "default is '%(add-upstream-vcs)s'",
         'aliases':
             "Whether to expand gbp specific aliases like `salsa:`,"
             "default is '%(aliases)s'",
index ae8bbc2856befa9b69c88bf033e1c49dcaec91d2..7e02f0e27e45ba2c7b563db616a9152040d2b292 100755 (executable)
@@ -22,6 +22,7 @@
 import re
 import sys
 import os
+import yaml
 from gbp.config import (GbpOptionParser, GbpOptionGroup)
 from gbp.deb.git import DebianGitRepository
 from gbp.git import (GitRepository, GitRepositoryError)
@@ -102,6 +103,24 @@ def repo_to_url(repo):
         return repo
 
 
+def add_upstream_vcs(repo):
+    upstream_info = os.path.join('debian', 'upstream', 'metadata')
+    if not os.path.exists(upstream_info):
+        gbp.log.warn("No upstream metadata, can't track upstream repo")
+        return
+
+    with open(upstream_info) as f:
+        metadata = yaml.safe_load(f)
+        url = metadata.get('Repository', None)
+
+    if url is None:
+        gbp.log.warn("No repository in metadata, can't track upstream repo")
+        return
+
+    gbp.log.info(f"Adding upstream vcs at {url} as additional remote")
+    repo.add_remote_repo('upstreamvcs', url, fetch=True)
+
+
 def build_parser(name):
     try:
         parser = GbpOptionParser(command=os.path.basename(name), prefix='',
@@ -112,8 +131,10 @@ def build_parser(name):
 
     branch_group = GbpOptionGroup(parser, "branch options", "branch tracking and layout options")
     cmd_group = GbpOptionGroup(parser, "external command options", "how and when to invoke hooks")
+    uvcs_group = GbpOptionGroup(parser, "upstream vcs options", "upstream vcs options")
     parser.add_option_group(branch_group)
     parser.add_option_group(cmd_group)
+    parser.add_option_group(uvcs_group)
 
     branch_group.add_option("--all", action="store_true", dest="all", default=False,
                             help="track all branches, not only debian and upstream")
@@ -129,6 +150,8 @@ def build_parser(name):
                                      "default is '%(postclone)s'")
     cmd_group.add_boolean_config_file_option(option_name="hooks", dest="hooks")
 
+    uvcs_group.add_boolean_config_file_option(option_name="add-upstream-vcs", dest='add_upstream_vcs')
+
     parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
                       help="verbose command execution")
     parser.add_config_file_option(option_name="color", dest="color", type='tristate')
@@ -216,6 +239,9 @@ def main(argv):
             if options.defuse_gitattributes.is_on() or not repo_setup.check_gitattributes(repo, 'HEAD'):
                 repo_setup.setup_gitattributes(repo)
 
+        if options.add_upstream_vcs:
+            add_upstream_vcs(repo)
+
         if postclone:
             Hook('Postclone', options.postclone,
                  extra_env={'GBP_GIT_DIR': repo.git_dir},
index a0678df474d0cfd84cb6e497637e8860aa64c3f0..ada301cbbd7a18685f82c8a50896bf73a72f5414 100644 (file)
@@ -61,10 +61,12 @@ class TestClone(ComponentTestBase):
         """Test that cloning from vcs-git urls works"""
         dest = os.path.join(self._tmpdir,
                             'cloned_repo')
-        ret = clone(['arg0', "vcsgit:libvirt-glib", dest])
+        ret = clone(['arg0', "--add-upstream-vcs", "vcsgit:libvirt-glib", dest])
         self.assertEquals(ret, 0)
         cloned = ComponentTestGitRepository(dest)
         self._check_repo_state(cloned, 'debian/sid', ['debian/sid', 'upstream/latest'])
+        assert cloned.has_remote_repo("upstreamvcs")
+        assert 'upstreamvcs/master' in cloned.get_remote_branches()
 
     @skipUnless(os.getenv("GBP_NETWORK_TESTS"), "network tests disabled")
     def test_clone_vcsgit_fail(self):