From e19a8e92bcf167385f46270b486ff2179ed891f4 Mon Sep 17 00:00:00 2001 From: Keith Short Date: Fri, 14 May 2021 14:36:25 -0600 Subject: zmake: Fall back to VCSID for version information The git repository information is not available to ebuilds after doing a "cros-workon- start" on the package. Fall back to the VCSID in this case. BUG=b:188530338 BRANCH=none TEST=emerge zephyr-build-tools TEST=zmake testall TEST="cros-workon-volteer-zephyr start chromeos-zephyr" and "emerge-volteer-zephyr chromeos-zephyr" Cq-Depend: chromium:2902154 Signed-off-by: Keith Short Change-Id: I03227005097b114061514ef1f1dcd79f242a1bf7 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2903906 Reviewed-by: Jeremy Bettis Reviewed-by: Simon Glass --- zephyr/zmake/zmake/version.py | 47 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/zephyr/zmake/zmake/version.py b/zephyr/zmake/zmake/version.py index 9128dd2bff..6cc37f95d4 100644 --- a/zephyr/zmake/zmake/version.py +++ b/zephyr/zmake/zmake/version.py @@ -3,12 +3,16 @@ # found in the LICENSE file. import subprocess +import os import zmake.util as util def _get_num_commits(repo): - """Get the number of commits that have been made in a Git repository. + """Get the number of commits that have been made. + + If a Git repository is available, return the number of commits that have + been made. Otherwise return a fixed count. Args: repo: The path to the git repo. @@ -16,14 +20,25 @@ def _get_num_commits(repo): Returns: An integer, the number of commits that have been made. """ - result = subprocess.run(['git', '-C', repo, 'rev-list', 'HEAD', '--count'], - check=True, stdout=subprocess.PIPE, - encoding='utf-8') - return int(result.stdout) + try: + result = subprocess.run(['git', '-C', repo, 'rev-list', 'HEAD', + '--count'], + check=True, stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, encoding='utf-8') + except subprocess.CalledProcessError: + commits = '9999' + else: + commits = result.stdout + + return int(commits) def _get_revision(repo): - """Get the index's current revision of a Git repo. + """Get the current revision hash. + + If a Git repository is available, return the hash of the current index. + Otherwise return the hash of the VCSID environment variable provided by + the packaging system. Args: repo: The path to the git repo. @@ -31,10 +46,22 @@ def _get_revision(repo): Returns: A string, of the current revision. """ - result = subprocess.run(['git', '-C', repo, 'log', '-n1', '--format=%H'], - check=True, stdout=subprocess.PIPE, - encoding='utf-8') - return result.stdout + try: + result = subprocess.run(['git', '-C', repo, 'log', '-n1', + '--format=%H'], + check=True, stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, encoding='utf-8') + except subprocess.CalledProcessError: + # Fall back to the VCSID provided by the packaging system. + # Format is 0.0.1-r425-032666c418782c14fe912ba6d9f98ffdf0b941e9 for + # releases and 9999-032666c418782c14fe912ba6d9f98ffdf0b941e9 for + # 9999 ebuilds. + vcsid = os.environ.get('VCSID', '9999-unknown') + revision = vcsid.rsplit('-', 1)[1] + else: + revision = result.stdout + + return revision def get_version_string(project, zephyr_base, modules, static=False): -- cgit v1.2.1