[gnome-continuous-yocto/gnomeostree-3.28-rocko: 6067/8267] bitbake: fetch/git: add support for keeping extra refs for shallow



commit 8144f6f408a77b7fb3367a91c55288b977a9bf88
Author: Christopher Larson <kergoth gmail com>
Date:   Sat May 13 02:46:30 2017 +0500

    bitbake: fetch/git: add support for keeping extra refs for shallow
    
    By default, all unused refs (branches & tags) are removed from the repository,
    as shallow processing scales with the number of refs it has to process. Add
    the ability to explicitly specify additional refs to keep. This is
    particularly useful for recipes with custom checkout processes, or whose
    git-based versioning requires a tag be available (i.e. for `git describe
    --tags`). The new `BB_GIT_SHALLOW_EXTRA_REFS` variable is a space-separated
    list of refs, fully specified, and support wildcards.
    
    Example usages:
    
        BB_GIT_SHALLOW_EXTRA_REFS = "refs/tags/v1.0"
        BB_GIT_SHALLOW_EXTRA_REFS += "refs/heads/*"
    
    (Bitbake rev: 1771934cd9f8b5847c6fcae0a906fb99d6b0db16)
    
    Signed-off-by: Christopher Larson <chris_larson mentor com>
    Signed-off-by: Richard Purdie <richard purdie linuxfoundation org>

 bitbake/lib/bb/fetch2/git.py  |   22 ++++++++++++++++++++-
 bitbake/lib/bb/tests/fetch.py |   42 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 1 deletions(-)
---
diff --git a/bitbake/lib/bb/fetch2/git.py b/bitbake/lib/bb/fetch2/git.py
index 250109b..aa972c5 100644
--- a/bitbake/lib/bb/fetch2/git.py
+++ b/bitbake/lib/bb/fetch2/git.py
@@ -72,6 +72,7 @@ Supported SRC_URI options are:
 
 import collections
 import errno
+import fnmatch
 import os
 import re
 import subprocess
@@ -180,6 +181,7 @@ class Git(FetchMethod):
             ud.cloneflags += " --mirror"
 
         ud.shallow = d.getVar("BB_GIT_SHALLOW") == "1"
+        ud.shallow_extra_refs = (d.getVar("BB_GIT_SHALLOW_EXTRA_REFS") or "").split()
 
         depth_default = d.getVar("BB_GIT_SHALLOW_DEPTH")
         if depth_default is not None:
@@ -265,8 +267,13 @@ class Git(FetchMethod):
                 if depth:
                     tarballname = "%s-%s" % (tarballname, depth)
 
+            shallow_refs = []
             if not ud.nobranch:
-                tarballname = "%s_%s" % (tarballname, "_".join(sorted(ud.branches.values())).replace('/', 
'.'))
+                shallow_refs.extend(ud.branches.values())
+            if ud.shallow_extra_refs:
+                shallow_refs.extend(r.replace('refs/heads/', '').replace('*', 'ALL') for r in 
ud.shallow_extra_refs)
+            if shallow_refs:
+                tarballname = "%s_%s" % (tarballname, "_".join(sorted(shallow_refs)).replace('/', '.'))
 
             fetcher = self.__class__.__name__.lower()
             ud.shallowtarball = '%sshallow_%s.tar.gz' % (fetcher, tarballname)
@@ -408,6 +415,19 @@ class Git(FetchMethod):
         # Map srcrev+depths to revisions
         shallow_revisions = runfetchcmd("%s rev-parse %s" % (ud.basecmd, " ".join(to_parse)), d, 
workdir=dest).splitlines()
 
+        # Apply extra ref wildcards
+        all_refs = runfetchcmd('%s for-each-ref "--format=%%(refname)"' % ud.basecmd,
+                               d, workdir=dest).splitlines()
+        for r in ud.shallow_extra_refs:
+            if not ud.bareclone:
+                r = r.replace('refs/heads/', 'refs/remotes/origin/')
+
+            if '*' in r:
+                matches = filter(lambda a: fnmatch.fnmatchcase(a, r), all_refs)
+                shallow_branches.extend(matches)
+            else:
+                shallow_branches.append(r)
+
         # Make the repository shallow
         shallow_cmd = ['git', 'make-shallow', '-s']
         for b in shallow_branches:
diff --git a/bitbake/lib/bb/tests/fetch.py b/bitbake/lib/bb/tests/fetch.py
index 0b0116b..3e2ce53 100644
--- a/bitbake/lib/bb/tests/fetch.py
+++ b/bitbake/lib/bb/tests/fetch.py
@@ -1293,6 +1293,48 @@ class GitShallowTest(FetcherTest):
         with self.assertRaises(bb.fetch2.FetchError):
             self.fetch()
 
+    def test_shallow_extra_refs(self):
+        self.add_empty_file('a')
+        self.add_empty_file('b')
+        self.git('branch a_branch', cwd=self.srcdir)
+        self.assertRefs(['master', 'a_branch'], cwd=self.srcdir)
+        self.assertRevCount(2, cwd=self.srcdir)
+
+        self.d.setVar('BB_GIT_SHALLOW_EXTRA_REFS', 'refs/heads/a_branch')
+        self.fetch_shallow()
+
+        self.assertRefs(['master', 'origin/master', 'origin/a_branch'])
+        self.assertRevCount(1)
+
+    def test_shallow_extra_refs_wildcard(self):
+        self.add_empty_file('a')
+        self.add_empty_file('b')
+        self.git('branch a_branch', cwd=self.srcdir)
+        self.git('tag v1.0', cwd=self.srcdir)
+        self.assertRefs(['master', 'a_branch', 'v1.0'], cwd=self.srcdir)
+        self.assertRevCount(2, cwd=self.srcdir)
+
+        self.d.setVar('BB_GIT_SHALLOW_EXTRA_REFS', 'refs/tags/*')
+        self.fetch_shallow()
+
+        self.assertRefs(['master', 'origin/master', 'v1.0'])
+        self.assertRevCount(1)
+
+    def test_shallow_missing_extra_refs(self):
+        self.add_empty_file('a')
+        self.add_empty_file('b')
+
+        self.d.setVar('BB_GIT_SHALLOW_EXTRA_REFS', 'refs/heads/foo')
+        with self.assertRaises(bb.fetch2.FetchError):
+            self.fetch()
+
+    def test_shallow_missing_extra_refs_wildcard(self):
+        self.add_empty_file('a')
+        self.add_empty_file('b')
+
+        self.d.setVar('BB_GIT_SHALLOW_EXTRA_REFS', 'refs/tags/*')
+        self.fetch()
+
     if os.environ.get("BB_SKIP_NETTESTS") == "yes":
         print("Unset BB_SKIP_NETTESTS to run network tests")
     else:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]