aboutsummaryrefslogtreecommitdiff
path: root/build-aux
diff options
context:
space:
mode:
authorfrosty <frostinator33@airmail.cc>2026-10-26 10:21:57 -0400
committerfrosty <frostinator33@airmail.cc>2026-10-26 10:21:57 -0400
commit74aecd8b0986d07b50b7ce310d64e645b8b54e38 (patch)
tree560e6434f183eb54cd779936f3a4c9575146a066 /build-aux
parent3d7e90d401da02d430a87db0ec7e1db3a6f1a68f (diff)
parent4436001bb361ad4f9e4e05819214ecf0ea77c063 (diff)
Merge 'upstream/master'
Diffstat (limited to 'build-aux')
-rw-r--r--build-aux/version.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/build-aux/version.py b/build-aux/version.py
new file mode 100644
index 00000000..45fdfcd3
--- /dev/null
+++ b/build-aux/version.py
@@ -0,0 +1,80 @@
+import argparse
+import subprocess
+import re
+
+
+def compute_version_from_file(file):
+ try:
+ with open(file) as f:
+ version_from_file = f.read().strip()
+ if version_from_file != "":
+ if version_from_file.startswith("RELEASE "):
+ return version_from_file[8:].strip()
+ if version_from_file.startswith("PRERELEASE "):
+ return version_from_file[11:].strip()
+ return version_from_file
+ except FileNotFoundError:
+ pass
+ return None
+
+
+def compute_version_from_git(git_repo, git):
+ try:
+ git_release_tag = subprocess.check_output([git, "describe", "--tags", "--abbrev=0"],
+ cwd=git_repo, text=True).strip()
+ if re.match("^v?([0-9]+[.]?[0-9]*[.]?[0-9]*)(-[.0-9A-Za-z-]+)?([+][.0-9A-Za-z-]+)?$", git_release_tag) is None:
+ return None
+ git_describe = subprocess.check_output([git, "describe", "--tags"], cwd=git_repo, text=True).strip()
+ if git_release_tag == git_describe:
+ return git_release_tag[1:]
+ matches = re.match("^.*-([0-9]+)-g([0-9a-f]+)$", git_describe)
+ if matches is None:
+ return None
+ git_tag_offset = matches.groups()[0]
+ git_commit_hash = matches.groups()[1]
+ git_commit_time = subprocess.check_output([git, "show", "--format=%cd", "--date=format:%Y%m%d", "-s"],
+ cwd=git_repo, text=True).strip()
+ return "%s~git%s.%s.%s" % (git_release_tag[1:], git_tag_offset, git_commit_time, git_commit_hash)
+ except subprocess.CalledProcessError:
+ pass
+ return None
+
+
+def compute_version(file, git_repo, git):
+ version_from_file = compute_version_from_file(file)
+ if version_from_file is not None:
+ return version_from_file
+ version_from_git = compute_version_from_git(git_repo, git)
+ if version_from_git is not None:
+ return version_from_git
+ return ""
+
+
+def main():
+ p = argparse.ArgumentParser(description="Compute the Dino version")
+ p.add_argument("--git-repo", help="Path to checked out git repository")
+ p.add_argument("--git", help="Path to git executable", default="git")
+ p.add_argument("--depfile", help="Create depfile")
+ p.add_argument("--deptarget", help="Target in depfile")
+ p.add_argument("version_file", metavar="VERSION_FILE",
+ help="Use this file's contents as version if the file exists")
+ args, unknown = p.parse_known_args()
+ if args.depfile:
+ with open(args.depfile, mode='w') as f:
+ f.write("version.stamp: ")
+ f.write(args.git_repo + "/VERSION ")
+ f.write(args.git_repo + "/.git/HEAD ")
+ try:
+ with open(args.git_repo + "/.git/HEAD") as f2:
+ head = f2.read().strip()
+ if head.startswith("ref:"):
+ f.write(args.git_repo + "/.git/" + head[4:].strip())
+ except FileNotFoundError:
+ pass
+ else:
+ version = compute_version(args.version_file, args.git_repo, args.git)
+ print(version)
+
+
+if __name__ == "__main__":
+ main()