[OE-core] [PATCH 3/3] recipetool: create: being able to set branch when revision is provided

Chang Rebecca Swee Fun rebecca.swee.fun.chang at intel.com
Thu Jul 13 03:33:56 UTC 2017


When recipetool create is run on a git URL and a revision specified
that is not on master, and "branch=" isn't already in the URL, then
we should get the correct branch and append the branch to the URL.

If the revision was found on multiple branches, we will display error
to inform user to provide a correct branch and exit.

[YOCTO #11389]

Signed-off-by: Chang Rebecca Swee Fun <rebecca.swee.fun.chang at intel.com>
---
 scripts/lib/recipetool/create.py | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/scripts/lib/recipetool/create.py b/scripts/lib/recipetool/create.py
index ee1f678..a45b90d 100644
--- a/scripts/lib/recipetool/create.py
+++ b/scripts/lib/recipetool/create.py
@@ -423,6 +423,17 @@ def create_recipe(args):
         if res:
             srcrev = res.group(1)
             srcuri = rev_re.sub('', srcuri)
+        # Check whether branch info is provided
+        branch_re = re.compile(';branch=([^;]+)')
+        branch = branch_re.search(srcuri)
+        nobranch_re = re.compile(';nobranch=1')
+        nobranch = nobranch_re.search(srcuri)
+        if not branch and not nobranch and srcrev != '${AUTOREV}':
+            # Append nobranch=1 in the following conditions:
+            # 1. User did not set 'branch=' in srcuri, and
+            # 2. User did not set 'nobranch=1' in srcuri, and
+            # 3. Source revision is not '${AUTOREV}'
+            fetchuri = fetchuri + ';nobranch=1'
         tempsrc = tempfile.mkdtemp(prefix='recipetool-')
         srctree = tempsrc
         d = bb.data.createCopy(tinfoil.config_data)
@@ -457,6 +468,26 @@ def create_recipe(args):
                     if '<html' in f.read(100).lower():
                         logger.error('Fetching "%s" returned a single HTML page - check the URL is correct and functional' % fetchuri)
                         sys.exit(1)
+        # Check for branch info with SRCREV provided
+        if not branch and not nobranch and srcrev and (srcrev != '${AUTOREV}'):
+            cmd = 'git branch -r --contains'
+            try:
+                # Command to check branch using commit hash
+                check_branch, check_branch_err = bb.process.run('%s %s' % (cmd, srcrev), cwd=srctree)
+            except bb.process.ExecutionError as err:
+                logger.error(str(err))
+                sys.exit(1)
+            get_branch = [x.strip() for x in check_branch.splitlines()]
+            # Remove HEAD reference point and drop remote prefix
+            get_branch = [x.split('/', 1)[1] for x in get_branch if not x.startswith('origin/HEAD')]
+            if len(get_branch) == 1:
+                # If get_branch contains only ONE object, then store result into 'branch'
+                branch = get_branch[0]
+            else:
+                # If get_branch contains more than one objects, then display error and exit.
+                mbrch = '\n  ' + '\n  '.join(get_branch)
+                logger.error('Revision %s was found on multiple branches: %s\nPlease provide the correct branch in the source URL with ;branch=<branch> (and ensure you use quotes around the URL to avoid the shell interpreting the ";")' % (srcrev, mbrch))
+                sys.exit(1)
         if os.path.exists(os.path.join(srctree, '.gitmodules')) and srcuri.startswith('git://'):
             srcuri = 'gitsm://' + srcuri[6:]
             logger.info('Fetching submodules...')
@@ -586,6 +617,11 @@ def create_recipe(args):
 
     if not srcuri:
         lines_before.append('# No information for SRC_URI yet (only an external source tree was specified)')
+    # Check if users has provide a branch
+    append_branch = branch_re.search(srcuri)
+    if branch and not append_branch:
+        # Append the correct branch into SRC_URI
+        srcuri += (';branch=%s' % str(branch))
     lines_before.append('SRC_URI = "%s"' % srcuri)
     (md5value, sha256value) = checksums
     if md5value:
-- 
2.7.4




More information about the Openembedded-core mailing list