From 9f7ddeca3a5135d032f2aaaf66589279f3542176 Mon Sep 17 00:00:00 2001 From: Naushir Patuck Date: Fri, 6 Sep 2024 09:50:33 +0100 Subject: [PATCH] core: Add release version number to version string Signed-off-by: Naushir Patuck --- core/meson.build | 2 +- utils/version.py | 68 +++++++++++++++++++++++++----------------------- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/core/meson.build b/core/meson.build index 0de2f99f..fbc23068 100644 --- a/core/meson.build +++ b/core/meson.build @@ -39,7 +39,7 @@ if fs.is_file(dist_version_file) version_cmd += fs.read(dist_version_file) endif -version_cpp = vcs_tag(command : version_cmd, +version_cpp = vcs_tag(command : [version_cmd, meson.project_version()], replace_string: '@VER@', input : 'version.cpp.in', output : 'version.cpp', diff --git a/utils/version.py b/utils/version.py index 48d7e059..ed508f83 100755 --- a/utils/version.py +++ b/utils/version.py @@ -11,42 +11,46 @@ digits = 12 -def generate_version(): +def generate_version(git_sha): + if git_sha is None: + # Check if this is a git directory + r = subprocess.run(['git', 'rev-parse', '--git-dir'], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, text=True) + if r.returncode: + raise RuntimeError('Invalid git directory!') + + # Get commit id + r = subprocess.run(['git', 'rev-parse', '--verify', 'HEAD'], + stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True) + if r.returncode: + raise RuntimeError('Invalid git commit!') + + commit = r.stdout.strip('\n')[0:digits] + '-intree' + + # Check dirty status + r = subprocess.run(['git', 'diff-index', '--quiet', 'HEAD'], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, text=True) + if r.returncode: + commit = commit + '-dirty' + else: + commit = git_sha.lower().strip() + if any(c not in hexdigits for c in commit): + raise RuntimeError('Invalid git sha!') + + commit = commit[0:digits] + + return commit + + +if __name__ == "__main__": try: - if len(sys.argv) == 1: - # Check if this is a git directory - r = subprocess.run(['git', 'rev-parse', '--git-dir'], - stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, text=True) - if r.returncode: - raise RuntimeError('Invalid git directory!') - - # Get commit id - r = subprocess.run(['git', 'rev-parse', '--verify', 'HEAD'], - stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True) - if r.returncode: - raise RuntimeError('Invalid git commit!') - - commit = r.stdout.strip('\n')[0:digits] + '-intree' - - # Check dirty status - r = subprocess.run(['git', 'diff-index', '--quiet', 'HEAD'], - stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, text=True) - if r.returncode: - commit = commit + '-dirty' - else: - commit = sys.argv[1].lower().strip() - if any(c not in hexdigits for c in commit): - raise RuntimeError('Invalid git sha!') - - commit = commit[0:digits] + rpicam_apps_ver = '0.0.0' if len(sys.argv) < 2 else sys.argv[1] + git_sha = None if len(sys.argv) < 3 else sys.argv[2] + commit = generate_version(git_sha) except RuntimeError as e: print(f'ERR: {e}', file=sys.stderr) commit = '0' * digits + '-invalid' finally: - print(f'{commit} {datetime.now().strftime("%d-%m-%Y (%H:%M:%S)")}', end="") - - -if __name__ == "__main__": - generate_version() + print(f'v{rpicam_apps_ver}-{commit} {datetime.now().strftime("%d-%m-%Y (%H:%M:%S)")}', end="")