From 78fd269e6649000b48ef1ef3e86009bad00fa815 Mon Sep 17 00:00:00 2001 From: Mark Asselstine Date: Sun, 9 Oct 2022 19:26:24 -0400 Subject: bitbake: bitbake: bitbake-layers: checkout layer(s) branch when clone exists [YOCTO #7852] Fixes 'bitbake-layers layerindex-fetch --branch kirkstone meta-arm' not checking out the branch if the repo is already cloned and on a different branch. If a clone of a layer being added already exists check what branch it is on and if necessary attempt to switch to the given branch. If the switch fails to happen the git error will be reported. We also warn if there are uncommitted changes as the changes might go unnoticed and result in unexpected behaviors. (Bitbake rev: d2cb388f58a37db2149fad34e4572d954e6e5441) Signed-off-by: Mark Asselstine Signed-off-by: Alexandre Belloni Signed-off-by: Richard Purdie --- bitbake/lib/bblayers/layerindex.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'bitbake/lib') diff --git a/bitbake/lib/bblayers/layerindex.py b/bitbake/lib/bblayers/layerindex.py index 0ac8fd2ec7..ba91fac669 100644 --- a/bitbake/lib/bblayers/layerindex.py +++ b/bitbake/lib/bblayers/layerindex.py @@ -49,6 +49,31 @@ class LayerIndexPlugin(ActionPlugin): else: logger.plain("Repository %s needs to be fetched" % url) return subdir, layername, layerdir + elif os.path.exists(repodir) and branch: + """ + If the repo is already cloned, ensure it is on the correct branch, + switching branches if necessary and possible. + """ + base_cmd = ['git', '--git-dir=%s/.git' % repodir, '--work-tree=%s' % repodir] + cmd = base_cmd + ['branch'] + completed_proc = subprocess.run(cmd, text=True, capture_output=True) + if completed_proc.returncode: + logger.error("Unable to validate repo %s (%s)" % (repodir, stderr)) + return None, None, None + else: + if branch != completed_proc.stdout[2:-1]: + cmd = base_cmd + ['status', '--short'] + completed_proc = subprocess.run(cmd, text=True, capture_output=True) + if completed_proc.stdout.count('\n') != 0: + logger.warning("There are uncommitted changes in repo %s" % repodir) + cmd = base_cmd + ['checkout', branch] + completed_proc = subprocess.run(cmd, text=True, capture_output=True) + if completed_proc.returncode: + # Could be due to original shallow clone on a different branch for example + logger.error("Unable to automatically switch %s to desired branch '%s' (%s)" + % (repodir, branch, completed_proc.stderr)) + return None, None, None + return subdir, layername, layerdir elif os.path.exists(layerdir): return subdir, layername, layerdir else: -- cgit v1.2.3-54-g00ecf