Fall back to VERSION file if .git is not present
authorKirill Isakov <bootctl@gmail.com>
Thu, 31 Mar 2022 05:32:56 +0000 (11:32 +0600)
committerKirill Isakov <bootctl@gmail.com>
Thu, 31 Mar 2022 05:32:56 +0000 (11:32 +0600)
https://github.com/gsliepen/tinc/issues/358

VERSION [new file with mode: 0644]
version.py

diff --git a/VERSION b/VERSION
new file mode 100644 (file)
index 0000000..c0540cb
--- /dev/null
+++ b/VERSION
@@ -0,0 +1 @@
+1.1pre18
index ff491bb..e9419a0 100755 (executable)
@@ -1,12 +1,17 @@
 #!/usr/bin/env python3
 
-from sys import argv, exit
+from os import path, environ
+from sys import argv, stderr
 import subprocess as subp
 
 prefix = "release-"
+source_root = path.dirname(path.realpath(__file__))
+source_root = environ.get("MESON_SOURCE_ROOT", source_root)
 
 cmd = [
     "git",
+    "--git-dir",
+    path.join(source_root, ".git"),
     "describe",
     "--always",
     "--tags",
@@ -19,8 +24,13 @@ if "short" in argv:
 result = subp.run(cmd, stdout=subp.PIPE, encoding="utf-8")
 version = result.stdout
 
-if not result.returncode and version and version.startswith(prefix):
+if result.returncode or not version:
+    try:
+        with open(path.join(source_root, "VERSION"), "r") as f:
+            version = f.read().strip()
+    except OSError as e:
+        print("could not read version from file", e, file=stderr)
+elif version.startswith(prefix):
     version = version[len(prefix):].strip()
 
 print(version if version else "unknown", end="")
-exit(not version)