Skip to content

Commit

Permalink
Detect dirtiness in . to flag it too in version
Browse files Browse the repository at this point in the history
  • Loading branch information
mosteo committed Jan 16, 2024
1 parent 35ca20b commit 4ad0a4a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
6 changes: 4 additions & 2 deletions alire.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ semantic_versioning = { url = "https://github.com/alire-project/semantic_version
simple_logging = { url = "https://github.com/alire-project/simple_logging", commit = "3505dc645f3eef6799a486aae223d37e88cfc4d5" }
stopwatch = { url = "https://github.com/mosteo/stopwatch", commit = "f607a63b714f09bbf6126de9851cbc21cf8666c9" }

# To disable version updating, export ALR_VERSION_DONT_PATCH with any value (even empty)

# Before building, we add the commit to the version, for unique identification:
[[actions]]
type = "pre-build"
command = ["python3", "scripts/version-patcher.py"]

# Afterwards we leave it as unknown, so people manually building don't use a
# Afterwards we leave an extra note, so people manually building don't use a
# misleading commit.
[[actions]]
type = "post-build"
command = ["python3", "scripts/version-patcher.py", "unknown_commit"]
command = ["python3", "scripts/version-patcher.py", "_or_later"]
34 changes: 26 additions & 8 deletions scripts/version-patcher.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import re
import subprocess
import sys
Expand All @@ -9,17 +10,34 @@ def replace_version(filename, new_text):

new_content = re.sub(pattern, r'\g<1>' + new_text + r'\3', content)

with open(filename, 'w') as file:
file.write(new_content)
if new_content == content:
if new_text in content:
print(f"Note: version in {filename} already up to date")
else:
print(f"WARNING: failed to update version in {filename}")
else:
with open(filename, 'w') as file:
file.write(new_content)

# If a flag exists, skip any updating, just print a message and exit
if "ALR_VERSION_DONT_PATCH" in os.environ:
print("Note: skipping version update")
sys.exit(0)

# If there is an argument to the script, retrieve it here and use it as the new
# version
# dirty flag
if len(sys.argv) > 1:
commit = sys.argv[1]
dirty = sys.argv[1]
else:
# Find the short git commit of the repository in the current directory
commit = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').strip()
# Detect the current directory contains changes
if subprocess.call(['git', 'diff-index', '--quiet', 'HEAD', '--']) != 0:
dirty = "_dirty"
else:
dirty = ""

# Find the short git commit of the repository in the current directory
commit = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').strip()

# Replace the build version part with the short commit hash
print(f"Updating version in src/alire/alire-version.ads to commit {commit}...")
replace_version('src/alire/alire-version.ads', commit)
print(f"Updating version in src/alire/alire-version.ads to commit {commit}{dirty}...")
replace_version('src/alire/alire-version.ads', commit+dirty)
8 changes: 4 additions & 4 deletions src/alire/alire-version.ads
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package Alire.Version with Preelaborate is

-- Remember to update Alire.Index branch if needed too

-- NOTE: in the following version string, "unknown_commit" will be replaced
-- by `alr build` with the current commit, and reverted to "unknown_commit"
-- after build.
-- NOTE: in the following version string, the build part (after '+') will
-- be replaced by `alr build` with the current commit, and appended with
-- "_or_later" after build.

Current : constant String := "2.0-beta2+unknown_commit";
Current : constant String := "2.0-beta2+92896bd3_or_later";
-- 2.0.0-b1: first public release on the 2.0 branch
-- 1.2.1: build switches fix and other minor assorted fixes
-- 1.2.0: rpm speed-up, silence propagation warning, early switch parse
Expand Down

0 comments on commit 4ad0a4a

Please sign in to comment.