Fall back to VERSION file if .git is not present
[tinc] / version.py
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)