summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2018-01-24 10:25:48 -0500
committerScott Moser <smoser@ubuntu.com>2018-01-24 10:25:48 -0500
commit30597f28512fafbe25486df5865b628d859486c6 (patch)
tree7b7202fa7ed04579f7eef780bf63e98c8fb545cb /tools
parentdf182de41fd75bc0cf2b1de2b511c37527d45475 (diff)
downloadvyos-cloud-init-30597f28512fafbe25486df5865b628d859486c6.tar.gz
vyos-cloud-init-30597f28512fafbe25486df5865b628d859486c6.zip
tools/read-version: Fix read-version when in a git worktree.
read-version --json would report bad data when working in a worktree. This is just because in a worktree, .git is not a directory, but rather a metadata file that points to the another path. $ git worktree ../mytree $ cat ../mytree/.git gitdir: /path/to/cloud-init/.git/worktrees/mytree $ rm -Rf ../mytree; git worktree prune
Diffstat (limited to 'tools')
-rwxr-xr-xtools/read-version15
1 files changed, 14 insertions, 1 deletions
diff --git a/tools/read-version b/tools/read-version
index d9ed30da..3ea9e66e 100755
--- a/tools/read-version
+++ b/tools/read-version
@@ -45,6 +45,19 @@ def which(program):
return None
+def is_gitdir(path):
+ # Return boolean indicating if path is a git tree.
+ git_meta = os.path.join(path, '.git')
+ if os.path.isdir(git_meta):
+ return True
+ if os.path.exists(git_meta):
+ # in a git worktree, .git is a file with 'gitdir: x'
+ with open(git_meta, "rb") as fp:
+ if b'gitdir:' in fp.read():
+ return True
+ return False
+
+
use_long = '--long' in sys.argv or os.environ.get('CI_RV_LONG')
use_tags = '--tags' in sys.argv or os.environ.get('CI_RV_TAGS')
output_json = '--json' in sys.argv
@@ -52,7 +65,7 @@ output_json = '--json' in sys.argv
src_version = ci_version.version_string()
version_long = None
-if os.path.isdir(os.path.join(_tdir, ".git")) and which("git"):
+if is_gitdir(_tdir) and which("git"):
flags = []
if use_tags:
flags = ['--tags']