From 6fa23316e26a464dd4839f4102570b9e3a6af3da Mon Sep 17 00:00:00 2001 From: Douglas Schilling Landgraf Date: Thu, 6 Feb 2025 18:25:32 -0500 Subject: [PATCH] --version points to git hash Today ramalama just share version 0.5.5 (as example) but don't tell users and developer which commit it's using. Resolves: https://github.com/containers/ramalama/issues/754 Signed-off-by: Douglas Schilling Landgraf --- install.sh | 24 +++++++++++++++++++++++- ramalama/version.py | 25 ++++++++++++------------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/install.sh b/install.sh index 0642c7de..dab108a1 100755 --- a/install.sh +++ b/install.sh @@ -100,6 +100,28 @@ check_platform() { return 0 } +setup_version_file() { + local config_dir="$HOME/.config/ramalama" + local version_file="$config_dir/version" + + # Ensure ~/.config/ramalama/ exists + mkdir -p "$config_dir" + + # Try to get version from Git + if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then + version=$(git describe --tags --long --always) + # Format version properly (strip 'g' from commit hash) + if [[ "$version" =~ ([0-9]+\.[0-9]+\.[0-9]+)-([0-9]+)-g([a-f0-9]+) ]]; then + version="${BASH_REMATCH[1]}.dev${BASH_REMATCH[2]}+${BASH_REMATCH[3]}" + fi + fi + + # Save version to file + echo "$version" > "$version_file" + + echo "Saved version: $version in $version_file" +} + setup_ramalama() { local binfile="ramalama" local from_file="${binfile}" @@ -181,7 +203,7 @@ main() { trap cleanup EXIT setup_ramalama "$bindir" + setup_version_file } main "$@" - diff --git a/ramalama/version.py b/ramalama/version.py index 876b6435..e99fb3bd 100644 --- a/ramalama/version.py +++ b/ramalama/version.py @@ -1,16 +1,15 @@ -import importlib.metadata - -"""Version of RamaLamaPy.""" +import os +CONFIG_DIR = os.path.expanduser("~/.config/ramalama") +VERSION_FILE = os.path.join(CONFIG_DIR, "version") def version(): - try: - return importlib.metadata.version("ramalama") - except importlib.metadata.PackageNotFoundError: - return "0" - - return "0" - - -def print_version(args): - print("ramalama version %s" % version()) + """Reads the version from ~/.config/ramalama/version.""" + if os.path.exists(VERSION_FILE): + with open(VERSION_FILE, "r") as f: + return f.read().strip() + return f"cannot be detected, see if exists {VERSION_FILE}" + +def print_version(args=None): + """Prints the current version of the package.""" + print(f"ramalama version {version()}")