diff options
-rw-r--r-- | zephyr/zmake/zmake/version.py | 47 |
1 files 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): |